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 )