Every time I destroy an object in my game, I create a text display object indicating how many points I just earned. These float gently up the screen and also fade out.
I want to be sure I am freeing up everything correctly as I go, so would my best bet be something like:for i = #imagesTable, 1, -1 do
-- Here I do whatever I want with each image
-- Then if for whatever reason I want to remove the item, I set 'shouldDie' = true, and do the following:
if shouldDie == true then
local child = table.remove(imagesTable, i)
if child ~= nil then
child:removeSelf()
child = nil
end
end
end
(Based on sample code found on the forums by FFM).
I’ve been using for i,v in ipairs() do up to now, but have no idea how or if I can delete items in it safely while iterating through it.
I also have another problem - a list that contains animating sprite sheets (particle effects in fact) that I wish to remove when they finish animating. Rather than have a time counter for each one and a system such as above, what I’d done is setup a callback from when the animation finishes. To not have to iterate manually through the list to find the matching particle effect and remove it, I had been using the image as both the key and the value, and then to remove it I was just doing: imagesTable[object] = nil
object:removeSelf()
Is this acceptable and safe code?
[import]uid: 46639 topic_id: 13653 reply_id: 313653[/import]