I’m working on a side scroller that has a character that fires a bullet every time you hit spacebar, the problem I’m having is moving the bullet in the direction the character is facing (left or right).
I have a few of boolean variables to tell me which direction my character is facing: walkingRight | walkingLeft, so if my walkingRight == true, I want the bullet to travel +=10, and walkingLeft == true, bullet -= 10.
The problem is, when I fire facing left, the bullet moves left, but as soon as I turn right, that same bullets starts moving right.
Here is a snippet of AS3 code (Every Frame):
if(gamepad.fire2.isPressed){
// initiate bullet
var bullet = new Bullet();
bullet.x = _player.x;
bullet.y = _player.y;
/*_boundaries.*/addChild(bullet);
bullets.push(bullet);
}
for each(var bullet in bullets){
if(walkingRight || idleRight || jumpingRight){
bullet.x += 10;
trace("Bullet - Moving Right");
}
else if(walkingLeft || idleLeft || jumpingLeft){
bullet.x -= 10;
trace("Bullet - Moving Left");
}
}
I sure would appreciate any help from this as its for a college project.
Thanks
You may try something like this:
First create this class:
Then modify your code: