Remove all children from DisplayObject3D

5 Comments 7 October 2009

I sometimes use the below code when removing all children from a Sprite or MovieClip in AS3:

while(myContainer.numChildren)
{
  myContainer.removeChildAt(0);
}

Here is the Papervision equivalent for removing children from a DisplayObject3D:

for(var o:Object in myContainer.children)
{
  myContainer.removeChild(myContainer.children[o]);
}

Posted in Papervision

5 Responses to Remove all children from DisplayObject3D

  1. 12 October 2009
    rauri

    while(myContainer.numChildren) myContainer.removeChildAt(0);

    cheers for the papervision version…

  2. 12 October 2009
    FlashMonkey

    Cheers… :)

  3. 12 October 2009
    rauri

    and don’t forget to remove those eventlisteners within myContainer.getChildAt(0->numChildren-1)
    and within myContainer.getChildAt(0->numChildren-1).getChildAt(0->numChildren-1) and etc…

    surely there’s a better way?

    i often make a kill function within any class that kills all event listeners, is optionally called when you want to disable a class, and is automatically called if you remove an instance of the class from the displaylist since it listens for the removed from stage event.

  4. 14 October 2009
    FlashMonkey

    I have an Abstract class that extend Sprite and contains a public dispose() method. That way you can do something like this:

    while(myContainer.numChildren)
    {
    AbstractDisplay(myContainer.getChildAt(0)).dispose();
    myContainer.removeChildAt(0);
    }

    That way it doesn’t matter what your Class is, or even if it contains an override of the dispose method, as long as somewhere along the inheritance line it extends AbstractDisplay. I also always set useWeakReference to true when adding event listeners.

    I got the Abstract class from Shane McCartney.

  5. 22 December 2009
    joshk

    good stuff mr. flashmonkey

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>