Saturday, August 9, 2008

recursive removeChild AS3 (unloadMovie)

UPDATE: From flash player 11 up, you use the new mySprite.removeChildren(); method (Thanks OMA!) for targeting older versions, the code below is still relevant...

you may be tempted to use a for loop to recursively remove movieclips or sprites nested in another display object.
but this single line will take care of it for you:

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

8 comments:

Valentijn Langendorff said...

Thanx for this ! Saved my day!
info AT multimediaal DOT nl

Dave said...

Yeah!!!

Tabby said...

Saved me a ton of time and frustration. Thanks.

Riad Louna said...

Thank YOU!

Muhammad said...

This is not recursive, its iterative.

A recursive function should remove the children of children, and their children (if they exist).

Oliver said...

Thanks for the clarification Muhammad! But I guess I want to make the post easily findable by google and people would probably be searching "recursive"... Also, the children of the children are deleted with this example and picked up by the garbage collection, just not recursive.

OMA said...

Recent versions of Flash and Adobe AIR now include a native method called removeChildren() which is faster and really recursive:

mySprite.removeChildren();

OMA said...
This comment has been removed by the author.