Delete Object after 5 Seconds

I was working on some stuff and I couldn’t figure out the reason it wasnt letting me delete the object after a 5 second timer!

test.collision = function(self, event)                         if ( event.phase == "began" ) then                             timer.performWithDelay(5000, function()                                 self:removeSelf()                                 self=nil                             end)                         end                     end                              test:addEventListener( "collision", test )  

I dont understand why it doesnt work but im getting this error

attempt to call method 'removeSelf' (a nil value)  

Any help with be greatly appreciated!

If this is the exact code you’re using, it doesn’t appear as though you have set the iterations for your timer. You might try this:

test.collision = function(self, event) if ( event.phase == "began" ) then timer.performWithDelay(5000, function() self:removeSelf() self=nil end, 1) end end test:addEventListener( "collision", test )

I still get the same error, i think the third iteration is optional. It runs the timer after collision and 5 seconds later that error pops up

Hmm…perhaps your object is still being acted on by the physics engine when it’s trying to be run? But that wouldn’t result in the error that you’re getting…

I usually remove objects with a function, so I modified your code and it seems to work. Try this:

local function destroyObj(obj) display.remove(obj) obj=nil end test = display.newRect(1120, -50,20,20) physics.addBody(test, "dynamic", {density = 1.0, friction = 10, bounce = .5}) game:insert(test) test.collision = function(self, event) if ( event.phase == "began" ) then timer.performWithDelay(5000, function() destroyObj(self) end, 1) end end test:addEventListener( "collision", test )

Are you sure “self” is still self inside that closuer/anonymous function?

The reason you are getting the error is because when two objects collide, there will be multiple collision events. Your code is removing the object on the first event (after 5 seconds). Five seconds after the second collision it tries to remove the object that is already removed. Calling display.remove(object) instead of object:removeSelf() does a check of the object and won’t try removing it if it doesn’t exist.

You can see this for yourself if you add a print statement in your collision delay function.

He’s right. Try using a variable so that it only happens once.
 
 

local function destroyObj(obj) display.remove(obj) obj=nil end test = display.newRect(1120, -50,20,20) physics.addBody(test, "dynamic", {density = 1.0, friction = 10, bounce = .5}) game:insert(test) test.hasCollidedOnce=false test.collision = function(self, event) if ( event.phase == "began" )and(test.hasCollidedOnce==false) then test.hasCollidedOnce=true -- doing this will prevent the delete from happening more than once timer.performWithDelay(5000, function() destroyObj(self) end, 1) end end test:addEventListener( "collision", test )

If this is the exact code you’re using, it doesn’t appear as though you have set the iterations for your timer. You might try this:

test.collision = function(self, event) if ( event.phase == "began" ) then timer.performWithDelay(5000, function() self:removeSelf() self=nil end, 1) end end test:addEventListener( "collision", test )

I still get the same error, i think the third iteration is optional. It runs the timer after collision and 5 seconds later that error pops up

Hmm…perhaps your object is still being acted on by the physics engine when it’s trying to be run? But that wouldn’t result in the error that you’re getting…

I usually remove objects with a function, so I modified your code and it seems to work. Try this:

local function destroyObj(obj) display.remove(obj) obj=nil end test = display.newRect(1120, -50,20,20) physics.addBody(test, "dynamic", {density = 1.0, friction = 10, bounce = .5}) game:insert(test) test.collision = function(self, event) if ( event.phase == "began" ) then timer.performWithDelay(5000, function() destroyObj(self) end, 1) end end test:addEventListener( "collision", test )

Are you sure “self” is still self inside that closuer/anonymous function?

The reason you are getting the error is because when two objects collide, there will be multiple collision events. Your code is removing the object on the first event (after 5 seconds). Five seconds after the second collision it tries to remove the object that is already removed. Calling display.remove(object) instead of object:removeSelf() does a check of the object and won’t try removing it if it doesn’t exist.

You can see this for yourself if you add a print statement in your collision delay function.

He’s right. Try using a variable so that it only happens once.
 
 

local function destroyObj(obj) display.remove(obj) obj=nil end test = display.newRect(1120, -50,20,20) physics.addBody(test, "dynamic", {density = 1.0, friction = 10, bounce = .5}) game:insert(test) test.hasCollidedOnce=false test.collision = function(self, event) if ( event.phase == "began" )and(test.hasCollidedOnce==false) then test.hasCollidedOnce=true -- doing this will prevent the delete from happening more than once timer.performWithDelay(5000, function() destroyObj(self) end, 1) end end test:addEventListener( "collision", test )