Remove Object After Collision

Hello,

I’m very new to corona and programming so please bear with me…

I’m trying to create a game where objects fall from the sky.

When the objects hit the ground, I want them to be removed after a period of time.

This is where I struggle with -

local function dropAction()
local crate= display.newImage(“crate.png”)
crate.width = 50
crate.height= 50
crate.y=-30
crate.x= math.random(5,1000)
crate.rotation=50
physics.addBody(crate, “dynamic”, {bounce=0.5})
local function onCollisionRemove(box, event)
    if(event.phase==“began”) then
    timer.performWithDelay(5000,box:removeSelf())
end
end
Runtime:addEventListener(crate.collision, onCollisionRemove(crate, crate.collision))
end

the bold part is the part where I try to remove the object after it’s had a collision, which obviously isn’t working…

i also tried this but it removes the object instantly -

**local function onLocalCollision( self, event )
        if ( event.phase == “began” ) then
timer.performWithDelay(5000, crate:removeSelf())
end
end

crate.collision = onLocalCollision
crate:addEventListener( “collision”, crate )**

Can someone please point me in the right direction??

Change box:removeSelf() to function() box:removeSelf() box = nil end and see if that works.

tried that but it gives me errors…

I think I’m not directing it to the object properly…

local function crateRemove()
crate.removeSelf()
crate=nil

local function onCollisionRemove(crate, event)
    if(event.phase==“began”) then
    timer.performWithDelay(5000,crateRemove())
end
end
Runtime:addEventListener(crate.collision, onCollisionRemove(crate, crate.collision))

Hi @danlinenberg,

The correct code is [lua]crate:removeSelf()[/lua] with a colon, since it’s an object method. Please try that and see if it solves the issue.

Best regards,

Brent Sorrentino

Yea forgot about it, but still no good…

it tells me I have a problem with the lines-

local function onCollisionRemove(crate, event)
    if(event.phase==“began”) then
    timer.performWithDelay(5000,crateRemove())

end

end

Again, am I directing it to the object properly through the function??

I tried this also:

local function crateRemove()
crate:removeSelf()
crate=nil
end
local function onLocalCollision( self, event )
        if ( event.phase == “began” ) then
timer.performWithDelay(5000, crateRemove())
end

end
crate.collision = onLocalCollision
crate:addEventListener( “collision”, crate )
end

But it removes the object instantly, instead waiting a few seconds after the collision…

Hi there,

Your last set of code is, I think, quite close.  There’s just an extra set of parentheses that’s causing the object to be removed instantly instead of with a delay.  Try this:

[lua]

local crate= display.newImage(“crate.png”)

crate.width = 50

crate.height= 50

crate.y=-30

crate.x= math.random(5,1000)

crate.rotation=50

physics.addBody(crate, “dynamic”, {bounce=0.5})

local function crateRemove()

   crate:removeSelf()

   crate=nil

end

local function onLocalCollision( self, event )

   if ( event.phase == “began” ) then

      timer.performWithDelay(5000, crateRemove)   – Notice that the () after crateRemove has been deleted.  This is important

   end

end

crate.collision = onLocalCollision

crate:addEventListener( “collision”, crate )

[/lua]

The reason why those () had to be deleted is because of how Lua works.  When you want to pass a reference to a function, like crateRemove, you simply use the variable name, without ().  When you want to actually call the function, you use the ().  For timer.performWithDelay to work, it needs a reference to the function.

Hope this helps!

  • Andrew

Can’t believe it was that simple.

Thank you!

Change box:removeSelf() to function() box:removeSelf() box = nil end and see if that works.

tried that but it gives me errors…

I think I’m not directing it to the object properly…

local function crateRemove()
crate.removeSelf()
crate=nil

local function onCollisionRemove(crate, event)
    if(event.phase==“began”) then
    timer.performWithDelay(5000,crateRemove())
end
end
Runtime:addEventListener(crate.collision, onCollisionRemove(crate, crate.collision))

Hi @danlinenberg,

The correct code is [lua]crate:removeSelf()[/lua] with a colon, since it’s an object method. Please try that and see if it solves the issue.

Best regards,

Brent Sorrentino

Yea forgot about it, but still no good…

it tells me I have a problem with the lines-

local function onCollisionRemove(crate, event)
    if(event.phase==“began”) then
    timer.performWithDelay(5000,crateRemove())

end

end

Again, am I directing it to the object properly through the function??

I tried this also:

local function crateRemove()
crate:removeSelf()
crate=nil
end
local function onLocalCollision( self, event )
        if ( event.phase == “began” ) then
timer.performWithDelay(5000, crateRemove())
end

end
crate.collision = onLocalCollision
crate:addEventListener( “collision”, crate )
end

But it removes the object instantly, instead waiting a few seconds after the collision…

Hi there,

Your last set of code is, I think, quite close.  There’s just an extra set of parentheses that’s causing the object to be removed instantly instead of with a delay.  Try this:

[lua]

local crate= display.newImage(“crate.png”)

crate.width = 50

crate.height= 50

crate.y=-30

crate.x= math.random(5,1000)

crate.rotation=50

physics.addBody(crate, “dynamic”, {bounce=0.5})

local function crateRemove()

   crate:removeSelf()

   crate=nil

end

local function onLocalCollision( self, event )

   if ( event.phase == “began” ) then

      timer.performWithDelay(5000, crateRemove)   – Notice that the () after crateRemove has been deleted.  This is important

   end

end

crate.collision = onLocalCollision

crate:addEventListener( “collision”, crate )

[/lua]

The reason why those () had to be deleted is because of how Lua works.  When you want to pass a reference to a function, like crateRemove, you simply use the variable name, without ().  When you want to actually call the function, you use the ().  For timer.performWithDelay to work, it needs a reference to the function.

Hope this helps!

  • Andrew

Can’t believe it was that simple.

Thank you!