Timer
You can do this:
for i = 1, 10 local obj = display.newCircle( 10, 10 \* i, 10 ) timer.performWithDelay( 100 \* i, function() display.remove( obj ) end ) end
But this is more efficient since it only defines one function instead of 10 closures:
local function onTimer( self ) display.remove( self ) end for i = 1, 10 local obj = display.newCircle( 10, 10 \* i, 10 ) obj.timer = onTimer timer.performWithDelay( 100 \* i, obj ) end
obj:removeSelf()
Side note. I never use removeSelf(). I prefer display.remove() because it is safer.
Setting locals to nil
If a variable is in fact local you don’t need to nil it. It will self-destruct when it falls out of scope.
Table Remove versus nil Assign
This:
group[sang.group]:remove(sang) sang:removeSelf() sang=nil
is less efficient than this:
group[sang.group] = nil -- assuming sang.group is not a number display.remove(sang)
assuming that sang.group is NOT a number. i.e. if group is not a numerically indexed table you can simply nil the entry you are removing.
The first version does a function call to remove, an unsafe call to removeSelf, and nils a local.
You can avoid all that using this kind of logic instead:
-- Just a demonstration of the concept not how you would do it specifically in your code -- -- My example uses a touch listener as the agent that decides when to delete the object, again -- just to demonstrate the concept of managing objects in a table. -- local trackingTable = {} local function onTouch( self, obj ) trackingTable[self] = nil -- clear the table index for this object (indexed by object reference) display.remove( self ) -- destroy the object... and done! end for 1, 100 do local obj = display.newCircle( math.random(10, 100), math.random(10, 100), 10 ) obj.touch = onTouch obj:addEventListener("touch") end
Regarding the use of the word group for a table
It is bad practice to use the word group anywhere in the name of a table, because it implies display group when in fact its just a table.
Just a note, but this made examining the code confusing and is still making this discussion a little hard to convey.
Timer Pause
Ah, yes I see. I figured you must be using a pausing mechanism, but I was having a little trouble with the naming and indexing.
Cool and hope this helps a little.
Cheers,
Ed