removeSelf - check to see if already done?

In my game, I have a coin that pops up. The player can tap the coin to get it - at which point I call coin:removeSelf(). No problems here.

However, I’m stuck with the game idea that I want the coin to disappear after a few seconds if the player doesn’t tap it quickly enough.

I used

timer.performWithDelay(2000, function() if object~=nil then object:removeSelf() end end)

thinking that if the object isn’t nil, then it hasn’t been removed yet, so I can go ahead and do it. However, I get the following error:

attempt to call method ‘removeSelf’ (a nil value)

Is there a “correct” way to check if an object has been removed already before I attempt to remove it via a timer? Or is there a better way to implement this game idea?

Thanks for your thoughts!

timer.performWithDelay( 2000, function() if object and object.removeSelf then object:removeSelf() object = nil end end )

This should do it.

Instead of removeSelf , you can also do display.remove( object ), which might not even require a check.

timer.performWithDelay( 2000, function() display.remove( object ) object = nil end )

Both ways worked great! Thanks!!

timer.performWithDelay( 2000, function() if object and object.removeSelf then object:removeSelf() object = nil end end )

This should do it.

Instead of removeSelf , you can also do display.remove( object ), which might not even require a check.

timer.performWithDelay( 2000, function() display.remove( object ) object = nil end )

Both ways worked great! Thanks!!