0
votes

I am currently trying to work out how I should go about shooting projectiles using Corona SDK. 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.

I want to be able to check if any of the bullets hit a sensor object ( I haven't implemented this but I know how to ) at the top of the screen and then destroy the bullet that hit the sensor but how do I check each instance and destroy them individually?

This is the basic structure that I have so far.

display.setStatusBar( display.HiddenStatusBar )
local physics = require( 'physics' )
physics.start()


local speed = -500
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 )
2

2 Answers

0
votes

This was suggested on the Corona forums by the Corona staff.

To remove each bullet that hits the sensor you need to give the projectile a 'type' of something along those lines. Note that you can use any word instead of 'type', but this is my preferred way.

projectile.type = 'bullet'

Then, you need to add an event listener to the sensor object that detects the collision, in this case it is an object called 'wall'. On collision you want to remove the other object that was in the collision ( the bullet ). You can do this like so.

local function wallCollision( event )
    if event.phase == 'began' then
        if event.other.type == 'bullet' then
            display.remove( event.other )
            event.other = nil
        end
    end
end

wall:addEventListener( 'collision', wallCollision )

'event.other' targets the other object involved in the collision event, in this case, the 'bullet'.

0
votes

Not sure this is what you are after, but the strategy to handle evolving multiple objects which can be removed later as a result of collision is:

  1. create the bullet display object, with a collision handler
  2. in the collision handler, if object needs to be removed then use removeSelf; other changes may require delayed change as explained in Modifying Objects.

So in your shoot function you would add, after projectile:setLinearVelocity:

projectile.collision = function (event) 
        ... 
        if remove then 
             self:removeSelf()
        end
        ... 
    end
projectile:addEventListener( "collision", projectile)

This adds the handler to each bullet. You could instead add just one handler for the sensor, it would be similar code which you would put right after creating the sensor, except that you remove the event.other instead of self:

sensor.collision = function (event) 
        ... 
        if remove then 
             event.other:removeSelf()
        end
        ... 
    end
sensor:addEventListener( "collision", sensor)