The Wonders of the removeMovieClip Method

There has been some discussion on the message boards recently about the removeMovieClip method of the MovieClip object.  This method has caused some confusion with some developers because it only works with certain MovieClip instances.  I thought I would take a couple minutes to explain the way this method works and why it sometimes doesn’t work.

The removeMovieClip method removes an instance of a MovieClip from the stage and it also deletes all of that instance’s methods and properties.  (This differs from the unloadMovie method, which simply removes the instance from the stage.)

The limitations of removeMovieClip are very simple.  You can’t remove a MovieClip when that clip has a depth below 0.  Any clip with a depth below 0 is protected from deletion.  While it appears that the method actually fails on MovieClips that are placed on the stage at author-time, the reality is that those author-time clips simply have a depth below 0.  Author-time clip depth actually begins at –16383.

Now that we know the secret to this MovieClip protection, we can use it to our advantage.  Any time you want to protect a MovieClip from being removed, place it at a depth less than 0.  Why would you want to do this?  Maybe you’re coding in a multi-developer environment and you don’t want someone else’s code to remove your clip.  Also you could use this information to remove author-time clips.  Simply use the swapDepths method to move the movie clip to a depth at or above 0.

[as]
myMovieClip_mc.swapDepths(0);
[/as]

Leave a Reply

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