removing movie clips
Instead of removing movie clips, I could have called it deleting, unloading, getting rid, making disappear... whatever!
Movie clips have the most important role in your flash movie, but there may be a time when you don't need them or don't want them to show up anymore. This is the time when you'll want to get rid of a movie clip.
You may think that hiding the movie clip is an option... using the _visible property can do it:
my_mc._visible = false;
but it's not the best idea. Even though you can't see it, the movie clip's still there, taking space, and probably reducing the flash player's performance. It may even be executing some code.
movie clips created dynamically
Getting rid of a dynamically created movie clip is not a big issue and is most of the times advisable. The function or method used to create it always has the opposite function or method to destruct it.
| create |
destruct |
| MovieClip.attachMovie() |
MovieClip.removeMovieClip() |
| MovieClip.duplicateMovieClip() |
MovieClip.removeMovieClip() |
| MovieClip.createEmptyMovieClip() |
MovieClip.removeMovieClip() |
| MovieClip.loadMovie() |
MovieClip.unloadMovie() |
| loadMovieNum() |
unloadMovieNum() |
| MovieClipLoader.loadClip() |
MovieClipLoader.unloadClip() |
This should be pretty easy to understand.
A movie clip created via createEmptyMovieClip() or placed on the stage via attachMovie() can be removed using removeMovieClip():
my_mc.attachMovie("ball", "ball", 1);
my_btn.onRelease = function()
{
my_mc.removeMovieClip();
};
movie clips created during authoring time
What if the movie clip was created during authoring time? removeMovieClip() doesn't seem to work... and unloadMovie(), although makes the movie clip disappear, still leaves its shell:
// movie clip called my_mc is on the stage
my_btn.onRelease = function()
{
my_mc.removeMovieClip();
};
// my_mc still there
// movie clip called my_mc is on the stage
my_btn.onRelease = function()
{
my_mc.unloadMovie();
};
// my_mc disappears, but its shell is still there:
another_btn.onRelease = function()
{
trace("my_mc: " + my_mc);
};
The solution lies in removeMovieClip() but it needs a little trick: you need to change the depths of the movie clip you want to remove before you remove it:
my_btn.onRelease = function()
{
my_mc.swapDepths(666); // specify any available depth
my_mc.removeMovieClip();
};
// in Flash MX 2004 it might be a good idea to use MovieClip.getNextHighestDepth()
// my_mc.swapDepths(some_mc.getNextHighestDepth());
// where some_mc is the timeline where the movie clip is
This was a very useful trick I learnt from senocular in his article Depths, now available at kirupa.com.
links
|