1a. The variable object1 contains a reference to the circle object.
1b. table[1] contains a reference to the circle object.
Tip: I know this was just a question, but ‘table’ is a invalid name for any variable since it is a reserved word.
2a. I agree this is problematic, but the object isn’t cleaned up till the next frame.
--- Dang local obj = display.newCircle( 10, 10, 10 ) print("here ==\> ", obj.removeSelf) obj:removeSelf() print("not gone? (not nil) ==\> ", obj.removeSelf)
-- Ahh local obj = display.newCircle( 10, 10, 10 ) print("here ==\> ", obj.removeSelf) obj:removeSelf() timer.performWithDelay(1, function() print("gone now (nil) ==\> ", obj.removeSelf) end )
2b. Having said that, I hate ‘removeSelf’ for this very reason. It is unsafe and constantly confuses new users. Thus I agree w/ @davebollinger and suggest that you use display.remove(obj).
Note: If you run into a crash however (caused by double call to obj:removeSelf(), it means you may have a problem in your game design.
3a. No… This is not safe because it causes holes:
local objs = {} objs[1] = display.newCircle( 10, 10, 10 ) objs[2] = display.newCircle( 10, 10, 10 ) objs[3] = display.newCircle( 10, 10, 10 ) display.remove( objs[2] ) objs[2] = nil -- Now objs contains { reference, nil, reference } print( #objs ) -- Prints 1, not 2 as you might expect.
3b. Yes… I suggest instead of using numeric indexes, use reference indexes.
-- Just an example to demonstrate concept... local objs = {} local tmp = display.newCircle( 10, 10, 10 ) objs[tmp] = tmp local tmp2 = display.newCircle( 10, 10, 10 ) objs[tmp2] = tmp2 local tmp = display.newCircle( 10, 10, 10 ) objs[tmp] = tmp objs[tmp2] = nil display.remove( tmp2 ) local count = 0 for k,v in pairs( objs ) do count = count + 1 end print( count ) -- prints 2
4. I don’t understand this question. Sorry.