Switching the direction of bullet spawn

Hey guys,

I am creating a 2D zombie shooter and have been having trouble switching the direction from the right side of the screen to the left. Here is my shoot function where I spawn the bullets along with set the X and Y. 

function shoot() local function collided(event) local phase = event.phase local other = event.other local function listener() display.remove(other) changeScore(1000) end if other.type=="zom" and phase == "began" then other:setSequence("dead") other:play() timer.performWithDelay( 750, listener ) else end end local new\_bullet = display.newCircle( level1, gun.x, gun.y, 5 ) new\_bullet.x = gun.x + 80 new\_bullet.y = gun.y - 17 local function move\_bullet() new\_bullet.x = new\_bullet.x + 30 if(new\_bullet.y \< 0) then new\_bullet:removeSelf( ) else timer.performWithDelay( 30, move\_bullet , 1 ) end end physics.addBody( new\_bullet, "dynamic" ) new\_bullet.isSensor = true new\_bullet.gravityScale = 0 new\_bullet:addEventListener("collision", collided) timer.performWithDelay( 33, move\_bullet , 1 ) end

First, I’m not sure what you’re trying to change the direction of. Can you be a bit more explicit in your description of the problem.

Second, you are trying to control the position of a dynamic physics body absolutely with a timer. Don’t do this; It will only cause you pain and frustration. You should be firing the bullet or making it a static sensor which you move absolutely.

As for exactly what I am trying to change is my local new_bullet display object although the reason I am having trouble with it is that it gets passed into move_bullet which must also move the bullet in the direction that it is fired.

As for your second point could you be a little more specific as I could go about fixing this. Thanks for the help.

So, when a bullet reaches one side of the screen you want to change it’s direction?

For objects which you control the speed and direction of, the easiest way is to simply have a property on that object with the velocity values. So, for your new_bullet object you would give it:

new\_bullet.vx = 10 new\_bullet.vy = 0

In the move_bullet function you just add the vx and vy values to the x and y values of the bullet, instead of 30 as you are doing now.

As you are creating a physics object and making it a sensor I assume you just want easy collision detection. That’s fine, but make sure the bullet is static so you’re not fighting with the Box2D physics engine all the time. As I said, that way lies dragons.

If you don’t want to worry about actually moving the bullet yourself, you can just set gravity to 0,0 and fire the bullet as a body. Then it will move under it’s own inertia and you only need to be concerned with the collision.

So this is currently what my game looks like below. Right now I am only generating an X and Y which matches the end of the riffle although I wish to switch it to the left side of the screen when the player and gun rotate. I will also need to change the physics so that the direction of the bullet travel is switched. 

kj0qK5w.png

This is actually a pretty simple math problem. Create a variable called “direction” and set it to either 1 or -1 depending on your character’s initial direction. As facing in the graphic, the direction value would be 1. If the character was facing the other way, it would be -1.

This variable needs to be scoped so that it can be seen throughout the entire scene. In other words declare it at the top of the scene. Every time you change your character’s direction simply multiply direction by -1:

direction = direction \* -1

Then in your move code, change this line:

new\_bullet.x = new\_bullet.x + 30

to

new\_bullet.x = new\_bullet.x + 30 \* direction

Rob

First, I’m not sure what you’re trying to change the direction of. Can you be a bit more explicit in your description of the problem.

Second, you are trying to control the position of a dynamic physics body absolutely with a timer. Don’t do this; It will only cause you pain and frustration. You should be firing the bullet or making it a static sensor which you move absolutely.

As for exactly what I am trying to change is my local new_bullet display object although the reason I am having trouble with it is that it gets passed into move_bullet which must also move the bullet in the direction that it is fired.

As for your second point could you be a little more specific as I could go about fixing this. Thanks for the help.

So, when a bullet reaches one side of the screen you want to change it’s direction?

For objects which you control the speed and direction of, the easiest way is to simply have a property on that object with the velocity values. So, for your new_bullet object you would give it:

new\_bullet.vx = 10 new\_bullet.vy = 0

In the move_bullet function you just add the vx and vy values to the x and y values of the bullet, instead of 30 as you are doing now.

As you are creating a physics object and making it a sensor I assume you just want easy collision detection. That’s fine, but make sure the bullet is static so you’re not fighting with the Box2D physics engine all the time. As I said, that way lies dragons.

If you don’t want to worry about actually moving the bullet yourself, you can just set gravity to 0,0 and fire the bullet as a body. Then it will move under it’s own inertia and you only need to be concerned with the collision.

So this is currently what my game looks like below. Right now I am only generating an X and Y which matches the end of the riffle although I wish to switch it to the left side of the screen when the player and gun rotate. I will also need to change the physics so that the direction of the bullet travel is switched. 

kj0qK5w.png

This is actually a pretty simple math problem. Create a variable called “direction” and set it to either 1 or -1 depending on your character’s initial direction. As facing in the graphic, the direction value would be 1. If the character was facing the other way, it would be -1.

This variable needs to be scoped so that it can be seen throughout the entire scene. In other words declare it at the top of the scene. Every time you change your character’s direction simply multiply direction by -1:

direction = direction \* -1

Then in your move code, change this line:

new\_bullet.x = new\_bullet.x + 30

to

new\_bullet.x = new\_bullet.x + 30 \* direction

Rob