I know you will probably say this has been covered multiple times, but still I am not quite sure what is the best approach to do this. I will try my best to explain my dilemma that might help other folks as well.
I’ve been trying different methods to clean a scene. I have read numerous answeres, but I am still no clear about this. This is my exeperiment using a slightly modified example from this forum:(note - I have previously forward declared parentGroup1 and rect1)
local function testFun(self, event) self.alpha = 0 end parentGroup1 = display.newGroup() local childGroup1 = display.newGroup( parentGroup1:insert( childGroup1 ) local childGroup2 = display.newGroup(); childGroup1:insert( childGroup2 ) childGroup1.name = "childGroup1" childGroup2.name = "childGroup2" rect1 = display.newRect( parentGroup1, 100, 100, 100, 100 ) rect1.name = "rect1" local rect2 = display.newRect( childGroup1, 200, 200, 100, 100 ) local rect3 = display.newRect( childGroup2, 300, 300, 100, 100 ) rect1.touch = testFun rect1:addEventListener("touch", rect1)
on scene:hide I use this function to see what happens:
for i = parentGroup1.numChildren, 1, - 1 do local child = parentGroup1[i] print(child.name) if child.touch then child:removeEventListener("touch", child) end if child then child:removeSelf() child = nil end end parentGroup1:removeSelf() parentGroup1 = nil
on scene:destroy I use the following to check if the cleanup is ok on rect1
if nil ~= rect1 then print("rect 1 still Here") end
and sure enough I get the “rect 1 still Here” when I leave the scene. I don’t think it should be there and I don’t get this if I just remove it like this:
rect1:removeSelf() rect1 = nil
but this way I would have remove each object one by one.
So my questions are:
- can I remove objects with other references just by removing and niling the table or a group?
- can I remove objects with iterating over a group or a table and if I can how, since on this example some references are obviously still there?
- is it enough to just do object:removeSelf() and object = nil, on an object with object:touch or object.touch eventListener on them or do I have to remove eventListener also.