Shooting Bullets ( Handling each bullet )

Hello!

First of all, I am new to these forums so sorry if this question is in the wrong location.

What I am trying to create is a simple bullet example so I can understand how to deal with them. I have written this short program.

display.setStatusBar( display.HiddenStatusBar ) local physics = require( 'physics' ) physics.start() physics.setDrawMode( 'debug' ) local contentW, contentH = display.contentWidth, display.contentHeight local bullet // Invisible wall that destroys any bullet that hits it. local wall = display.newRect( contentW / 2, 100, contentW, 30 ) physics.addBody( wall, 'static' ) wall.isSensor = true wall.type = 'wall' local function shoot( event ) if event.phase == 'began' then bullet = display.newRect( contentW / 2, contentH, 10, 30 ) physics.addBody( bullet, 'dynamic' ) bullet.gravityScale = 0 bullet.isSensor = true bullet.isBullet = true bullet.type = 'bullet' bullet:setLinearVelocity( 0, -300 ) end end local function wallCollision( event ) if event.phase == 'began' then if event.other.type == 'bullet' then display.remove( bullet ) end end end Runtime:addEventListener( 'touch', shoot ) wall:addEventListener( 'collision', wallCollision )

What I want to happen is that on touch, a bullet is created and fired upwards. When it hits a wall ( sensor ), I want the bullet to be removed. However, when you tap a few times, when the first bullet collides with the sensor, it removes the last bullet that was shot. If you don’t understand please test it for your self and tap twice with a short amount of time in between. 

I would also like to ask if this is the best way to go about doing this or is there a better way?

Thanks.

Hi @guitarmatt99,

The issue here is that you’re targeting the reference (pointer) to “bullet” when you call “display.remove()”. So it’s naturally removing the last instance of this, since each time you spawn a bullet, you’re assigning that new bullet to the reference. What you should do is simply reference the actual bullet that’s involved in the collision:

[lua]

display.remove( event.other )

event.other = nil

[/lua]

Take care,

Brent

I did actually try this, however I just used ‘other’ instead of ‘event.other’. Thanks for all the help you’ve given me, it is much appreciated! Now I can continue with my bigger project! :slight_smile:

Matthew.

Hi @guitarmatt99,

The issue here is that you’re targeting the reference (pointer) to “bullet” when you call “display.remove()”. So it’s naturally removing the last instance of this, since each time you spawn a bullet, you’re assigning that new bullet to the reference. What you should do is simply reference the actual bullet that’s involved in the collision:

[lua]

display.remove( event.other )

event.other = nil

[/lua]

Take care,

Brent

I did actually try this, however I just used ‘other’ instead of ‘event.other’. Thanks for all the help you’ve given me, it is much appreciated! Now I can continue with my bigger project! :slight_smile:

Matthew.