Remove all children from DisplayObject3D

2009 October 7
by FlashMonkey

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]);
}
5 Comments leave one →
2009 October 12
rauri permalink

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

cheers for the papervision version…

2009 October 12
FlashMonkey permalink

Cheers… :)

2009 October 12
rauri permalink

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.

2009 October 14
FlashMonkey permalink

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.

2009 December 22
joshk permalink

good stuff mr. flashmonkey

Leave A Comment

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS