Page 1 of 1

how to make an actor to create bullet towhere the anim face

PostPosted: Sat Apr 28, 2018 2:39 pm
by Ehman
Please I want to create a game in which the fighter creates bullet(probably from guns) which goes in the direction the fighter faces asumming
Code: Select all
key down "spacebar" =>create actor "bullet" x=+5; y=0
to create bullet to the right side of the player. Now, the player faces the down ward side of the game You know the bullet will still move in the
Code: Select all
+xvelocity
direction THANKS!
- EasyHuMaN (EHMAN)
I CAN'T WAIT TO SEE YOUR RESPONSE AND TO PLAY THE GAME (+ [ ] × ) :lol: :lol: :D :D :idea:

Re: how to make an actor to create bullet towhere the anim

PostPosted: Mon Apr 30, 2018 9:57 pm
by lcl
You could create a variable for tracking the direction the player is facing.

Create a new variable called facingDir. A value of 1 will tell us that the player is facing right, and a value of -1 will tell us that the player is facing left.

Player -> Create Actor -> Script Editor:
Code: Select all
facingDir = 1; // The player starts facing right

Player -> Key Down event for moving right -> Script Editor:
Code: Select all
// Your movement code is here
facingDir = 1;

Player -> Key Down event for moving left -> Script Editor:
Code: Select all
// Your movement code is here
facingDir = -1;

Player -> Key Down (Space) -> Script Editor:
Code: Select all
int bulletSpeed = 20;
Actor *newBullet = CreateActor( ... ); // Get a pointer to the new bullet actor created

// If facingDir is 1, xvelocity will be 20, and if facingDir is -1, xvelocity will be -20
newBullet->xvelocity = bulletSpeed * facingDir; // Set the xvelocity for the bullet

The lines that begin with "//" are comments.

If there's something that you find confusing, don't hesitate to ask, I will explain it to you. :)

Re: how to make an actor to create bullet towhere the anim

PostPosted: Tue May 01, 2018 6:37 am
by Ehman
You are a million in 1 bro.
what about facing up or down (y axis)?

Re: how to make an actor to create bullet towhere the anim

PostPosted: Tue May 01, 2018 9:29 am
by lcl
Oh, sorry, I thought you were making a platformer with basic shooting to left and right. But it's not that different if you want to shoot 4 ways (left, right, up, down).

Instead of -1 and 1 for left and right, which is nice for shooting only on the horizontal axis, we will now have to define 4 different directions. Let's choose these numbers:

  • 0 - right
  • 1 - up
  • 2 - left
  • 3 - down
At first look, it might seem a bit weird to start from right and go counter-clockwise from there, but there's a reason for that. Game Editor's angle system starts with 0 for right and rotates counter-clockwise from there, so 90 degrees for up, 180 for left and 270 for down. So, as you can see, our numbers align with that setup.

So, the next step is to just add the corresponding codes to the movement events to change the facingDir variable to the values listed above.

Now, you may not be familiar with angle and directional_velocity in Game Editor. These variables are actor variables (= variables, that each actor has their own values for. x and y are actor variables as well) that can be used together to move an actor in any direction by given angle with the speed of the given directional_velocity. For more information, check GE script reference: angle and GE script reference: directional_velocity.

Now, the only thing left is to adjust the shooting event. What we will do is to create a new bullet actor, set its angle equal to facingDir * 90, and then set it's directional_velocity to the speed we want to give to the bullet.
Code: Select all
int bulletSpeed = 20; // Temporary variable for the speed to give to the bullet
Actor *newBullet = CreateActor( ... ); // Get a pointer to the new bullet actor created

newBullet->angle = facingDir * 90; // Set bullet movement angle
newBullet->directional_velocity = bulletSpeed; // Set bullet movement speed

Re: how to make an actor to create bullet towhere the anim

PostPosted: Thu May 03, 2018 5:26 am
by tdmxross
I am not good at programming, but this is my way to make the player shoot in the direction he is facing. Have a look at the fireball actors too. :D

Re: how to make an actor to create bullet towhere the anim

PostPosted: Thu May 03, 2018 11:30 am
by lcl
tdmxross wrote:I am not good at programming, but this is my way to make the player shoot in the direction he is facing. Have a look at the fireball actors too. :D

Yes, your solution also works, but it's needlessly complex. You use 4 variables for shooting direction, while only 1 is enough. Same goes for the fireball actor, you don't need 4 of those. Also, instead of using Draw Actor to update the bullet's x or y every frame, you could just use xvelocity, yvelocity or directional_velocity to give the actor a velocity when you create it. :)

Re: how to make an actor to create bullet towhere the anim

PostPosted: Fri May 04, 2018 6:41 am
by tdmxross
lcl wrote:
tdmxross wrote:I am not good at programming, but this is my way to make the player shoot in the direction he is facing. Have a look at the fireball actors too. :D

Yes, your solution also works, but it's needlessly complex. You use 4 variables for shooting direction, while only 1 is enough. Same goes for the fireball actor, you don't need 4 of those. Also, instead of using Draw Actor to update the bullet's x or y every frame, you could just use xvelocity, yvelocity or directional_velocity to give the actor a velocity when you create it. :)


That's cool too! but this is one of my old projects, that's why it's so messed up :lol: . thanks for your reply! :D

Re: how to make an actor to create bullet towhere the anim

PostPosted: Fri May 04, 2018 2:51 pm
by Ehman
tdmxross wrote:I am not good at programming, but this is my way to make the player shoot in the direction he is facing. Have a look at the fireball actors too. :D

THANKS FOR YOUR CONTRIBUTION

Re: how to make an actor to create bullet towhere the anim

PostPosted: Fri May 04, 2018 2:58 pm
by Ehman
I thought of another variable 'facingY'

Add a new event:
create actor => Script editor:
Code: Select all
facingY = 0;

Facing up code:
Key down "up" => Script editor :
Code: Select all
facingY = - 1;
facingDir=0;

facing down code:
Code: Select all
 facingY = - 1;
facingDir=0;

Re: how to make an actor to create bullet towhere the anim

PostPosted: Fri May 04, 2018 3:01 pm
by Ehman
Just change the 'facingDir' to 'facingY' when you create this event:
Key down "space" => Script editor ( ... )

Re: how to make an actor to create bullet towhere the anim

PostPosted: Fri May 04, 2018 3:03 pm
by Ehman
Just change the 'facingDir' to 'facingY' when you create this event:
Key down "space" => Script editor ( ... )

Re: how to make an actor to create bullet towhere the anim

PostPosted: Sat May 05, 2018 2:45 pm
by Ehman
And how can I make a restart button when my player is out of vision?

Re: how to make an actor to create bullet towhere the anim

PostPosted: Tue May 08, 2018 2:46 pm
by tdmxross
Ehman wrote:And how can I make a restart button when my player is out of vision?

I think this will help!
Use A and D for movement, if the player leaves the view (Out of vision) a restart button will appear. you can then click on the restart button to restart the game.
check out the scripts and figure out how it works.

Re: how to make an actor to create bullet towhere the anim

PostPosted: Wed May 09, 2018 4:26 pm
by Ehman
Thanks

Re: how to make an actor to create bullet towhere the anim

PostPosted: Wed May 09, 2018 6:05 pm
by tdmxross
Ehman wrote:Thanks

NP :D