Search code examples
actionscript-3flashflash-builderflashdevelop

removeChild() on parent will remove its child?


I have a construction as below:

parent
   + child
   + child
   + child
   + sub-parent
         + sub-child
         + sub-child

For now, I use multiply of "removeChild()", to remove elements from scene one by one. Like this:

removeChild(sub-child);
removeChild(sub-child);
removeChild(sub-parent);
//and so on 

It's okay, but I found out that if I remove a parent all its children will disappear from scene too. (For now I do not know for sure ...)

How do you remove elements from a scene in the correct way ? As I do it now (one by one), or I could remove just a parent and my code will be a little bit shorter. Or is it the same thing?


Solution

  • Removing a display object from the stage will also remove all of that object's children. Think of it as a container that holds those children object. If you remove the container, you also remove the objects inside of it.

    However, if you still have references to those children objects, or have event listeners attached to them, they won't be garbage collected (they will stick around in memory executing any code associated with them). So you still need to make sure you clean up everything when you remove the parent object.