Shooting Projectiles ( Handling each bullet individually )

Hello! I am currently trying to work out how I should go about shooting projectiles. However, I don’t know the best way to go about doing this. I am guessing that you have to spawn instances of the same object and apply force to them but I don’t know the best way to do it or how I should handle each instance. I am still learning lua and just need some guidance on how to do it, any help will be appreciated.

This is the point I got up to, every time you click a projectile object  is spawned and travels upwards. I want to be able to check if any of the bullets hit a sensor at the top of the screen and then destroy them but how do I check each instance and destroy them individually?

display.setStatusBar( display.HiddenStatusBar ) local physics = require( 'physics' ) physics.start() local contentW, contentH = display.contentWidth, display.contentHeight -- Background local bg = display.newRect( 0, 0, contentW, contentH ) bg.anchorX = 0 bg.anchorY = 0 bg:setFillColor( 0, 1, 1 ) -- Ground local ground = display.newRect( 0, contentH - 50, contentW, 50 ) ground.anchorX = 0 ground.anchorY = 0 ground:setFillColor( 0, 0.8, 0 ) -- Hero local hero = display.newRect( contentW / 2, contentH / 2, 40, 40 ) hero:setFillColor( 1, 0, 0 ) function shoot( event )     if ( event.phase == 'began' ) then         local projectile = display.newRect( hero.x, hero.y, 10, 30 )     physics.addBody( projectile, 'dynamic' ) projectile.gravityScale = 0 projectile.isBullet = true projectile:setLinearVelocity( 0, -600 ) end end Runtime:addEventListener( 'touch', shoot )

Hi @guitarmatt99,

Your code looks pretty good so far. The next step should be to read the “Collision Detection” guide (link below). I suggest that you implement a local collision handler as shown under “Local Collision Handling”, and apply the collision listener to the sensor object (which you’ll need to create, and set as a sensor type obviously).

Also note that it’s best to use the “less is more” approach. While you could apply a collision listener to each bullet instance, that’s unnecessary and more work than needed. Why? Because each bullet will hit the sensor object, and thus the sensor object should be the one object that has a collision listener on it. Then, its listener will detect when other objects (bullets) hit it, and you can tell Corona to remove the “other object that was involved in the collision”, which is any instance of a bullet.

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Best regards,

Brent

Firstly, thanks for the reply and the information you’ve given me. However, I have partially figured this out myself and have actually posted another forum post in the ‘General Questions’ section as I wasn’t sure if this was posted in the correct place. I seem to have a very odd problem that is occurring and it would be great if you could take a quick look! 

http://forums.coronalabs.com/topic/52030-shooting-bullets-handling-each-bullet/

Matthew.

Hi @guitarmatt99,

Your code looks pretty good so far. The next step should be to read the “Collision Detection” guide (link below). I suggest that you implement a local collision handler as shown under “Local Collision Handling”, and apply the collision listener to the sensor object (which you’ll need to create, and set as a sensor type obviously).

Also note that it’s best to use the “less is more” approach. While you could apply a collision listener to each bullet instance, that’s unnecessary and more work than needed. Why? Because each bullet will hit the sensor object, and thus the sensor object should be the one object that has a collision listener on it. Then, its listener will detect when other objects (bullets) hit it, and you can tell Corona to remove the “other object that was involved in the collision”, which is any instance of a bullet.

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Best regards,

Brent

Firstly, thanks for the reply and the information you’ve given me. However, I have partially figured this out myself and have actually posted another forum post in the ‘General Questions’ section as I wasn’t sure if this was posted in the correct place. I seem to have a very odd problem that is occurring and it would be great if you could take a quick look! 

http://forums.coronalabs.com/topic/52030-shooting-bullets-handling-each-bullet/

Matthew.