will "removeSelf" on a display group remove all it's children?

Just checking in if there’s any changes/updates that might assist automatically removing child display objects from a group (versus having to get rid of them manually).  

Question : will “removeSelf” on a display group remove all it’s display objects children, included nested display groups under it?  (assuming we’re not working with composer at all here)

(I’m assuming it doesn’t, and you have to keep track of your children objects to manually remove, but just in case I’m missing something - any other tips/tricks welcome)

Yes, it should. Are you possibly seeing otherwise?

Brent

Hi Brent - no, it’s just I’ve always been keeping track of children and removing manually for a long time but hadn’t got around to checking if it’s necessary.  

So basically there’s no need to keep track of displayObjects within a display group then (including nested display groups of display objects within the parent display group) correct?  I can just go “myParentObject:removeSelf();  myParentObject = nil”  and this should be enough then right? 

thanks Greg

Hi Greg,

As usual, the proof is in the pudding. :slight_smile:

[lua]

local parentGroup1 = display.newGroup()

local childGroup1 = display.newGroup(); parentGroup1:insert( childGroup1 )

local childGroup2 = display.newGroup(); childGroup1:insert( childGroup2 )

local rect1 = display.newRect( parentGroup1,100,100,100,100 )

local rect2 = display.newRect( childGroup1,200,200,100,100 )

local rect3 = display.newRect( childGroup2,300,300,100,100 )

timer.performWithDelay( 1000, function() parentGroup1:removeSelf(); end )

[/lua]

Excellent, thanks Brent

Yes, it should. Are you possibly seeing otherwise?

Brent

Hi Brent - no, it’s just I’ve always been keeping track of children and removing manually for a long time but hadn’t got around to checking if it’s necessary.  

So basically there’s no need to keep track of displayObjects within a display group then (including nested display groups of display objects within the parent display group) correct?  I can just go “myParentObject:removeSelf();  myParentObject = nil”  and this should be enough then right? 

thanks Greg

Hi Greg,

As usual, the proof is in the pudding. :slight_smile:

[lua]

local parentGroup1 = display.newGroup()

local childGroup1 = display.newGroup(); parentGroup1:insert( childGroup1 )

local childGroup2 = display.newGroup(); childGroup1:insert( childGroup2 )

local rect1 = display.newRect( parentGroup1,100,100,100,100 )

local rect2 = display.newRect( childGroup1,200,200,100,100 )

local rect3 = display.newRect( childGroup2,300,300,100,100 )

timer.performWithDelay( 1000, function() parentGroup1:removeSelf(); end )

[/lua]

Excellent, thanks Brent

Brent, in your example is it still necessary to call rect1 = nil, rect2 = nil, rect3 = nil?  Or would parentGroup1 = nil do the trick?

Hi @JonPM,

If you remove the parent group, it should recursively dig down and remove all children. Of course, by setting it “nil”, do you mean clearing the group and its children from the display, or releasing the Lua memory pointers? As long as you haven’t tied up the local references (i.e. rect1, rect2) to some other variable(s) that remain in memory, i.e. put those references inside another table that remains in memory, the Lua garbage collector should do its job and you won’t have to worry about it.

Brent

Brent, in your example is it still necessary to call rect1 = nil, rect2 = nil, rect3 = nil?  Or would parentGroup1 = nil do the trick?

Hi @JonPM,

If you remove the parent group, it should recursively dig down and remove all children. Of course, by setting it “nil”, do you mean clearing the group and its children from the display, or releasing the Lua memory pointers? As long as you haven’t tied up the local references (i.e. rect1, rect2) to some other variable(s) that remain in memory, i.e. put those references inside another table that remains in memory, the Lua garbage collector should do its job and you won’t have to worry about it.

Brent