Actionscript 3: Remove all elements from a Movieclip

I’m always trying to empty out a clip and this is by far the best way :

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

or if you prefer to be verbose :

var total:uint = target_mc.numChildren;
for (var i:uint = 0; i < total; i++) {
target_mc.removeChildAt(0);
}

or if you prefer single function :

for (var i:uint = 0; i < target_mc.numChildren; i++) {
target_mc.removeChildAt(0);
i--;
}

Here is a user submitted version :

while (target_mc.numChildren > 0) {
target_mc.removeChildAt(target_mc.numChildren - 1);
}

Here is a one line version :

while (target_mc.numChildren != 0) target_mc.removeChildAt(0);

I think that’s it.

This entry was posted in Actionscript 3.0 and tagged . Bookmark the permalink.

2 Responses to Actionscript 3: Remove all elements from a Movieclip

  1. Jay Vanian says:

    You’re brilliant!

  2. That is some slick code.

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>