Code Tip: Removing Children from a MovieClip

I used to remove children of a movieclip like this:

for ( var i:int = 0; i < myMovie.numChildren; i ++ )
{
myMovie.removeChildAt ( i );
}

But it becomes problematic if myMovie has children placed all over the container without having any control over the depth at all. Since the approach mentioned above uses depth as its primary source of identification, it is difficult to get all the objects present within if the developer loses control over depth of each child element (ok I admit, I did that 🙂 and I complicated it for myself). So there was a point I was thinking that ok I made a mistake, just clear up my container movieclip.

Then I recently found out that removing all children from a movieclip can also be done through a while loop like this:

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

Boom. We are done. End of story.

Advertisements
Code Tip: Removing Children from a MovieClip

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s