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.