Another thing missing in ActionScript 2.0
July 1, 2006
I just remembered another frustrating thing in ActionScript 2.0
Dynamicly removing a MovieClip is not as easy as my_movieclip.removeMovieClip() or removeMovieClip(my_movieclip). These methods are used for removing MovieClips but sometimes they work, sometimes they don’t. It turns out that you can only remove MovieClips in a certain depth range. Very frustrating. Here is the code that I use to remove MovieClips.
function removeMC(mc:MovieClip):Void
{
//Flash only allows you to programmatically remove a movie clip instance if its depth is within a specific range.
//Any movie clip with a depth between 0 and 1048575 inclusive can be programmatically removed.
//Any other movie clip remains unaffected by removeMovieClip( ).
var mTemp:MovieClip = _root.getInstanceAtDepth(0);
mc.swapDepths(0);
mc.removeMovieClip();
if(mTemp != undefined) mTemp.swapDepths(0);
}
I hope this might help some people!


October 25, 2006 at 11:58 am
It works !!!
Thanks, you saved my life.
October 25, 2006 at 11:59 am
You’re welcome.
March 27, 2007 at 10:02 pm
You’ve saved my life, too! I’ve been trying to figure it out for hours!
Just a question – why does the depth of the movie clip has to be between 0 and 1048575 ? What’s wrong with ANY depth?
April 3, 2007 at 11:47 am
gah! I still need some more hand-holding. What do I edit in that code to include the name of the movie clip that I want to remove or do I just have to add the line removeMovieClip(this.blocker_mc); (supposing my movie clip is called blocker_mc)?
I tried it that way, but with no success. Do I need to name my clip “mTemp”? Sorry for a dumb question, I’m pretty sure.
April 3, 2007 at 12:00 pm
@Dragy: I don’t know why the Flash developers choose this seemingly random range of depth numbers.
I accepted it as a fact. In my opinion there’s far to much nonsense in Flash and Actionscript to waste my energy uncovering the motives. But if you happen to figure it out, please let me know!
@Cal,
there is not such a thing as a “dumb question“.
If the movieclip you want to remove is called blocker_mc you remove it by calling removeMC(blocker_mc).
Make sure you properly address the function and the movieclip, keep scope in mind. So removeMC(blocker_mc) would only work if both the function declaration removeMC and the variable block_mc are in the same scope where the “removeMC(blocker_mc)“-statement executes.
April 3, 2007 at 12:19 pm
ah thanks! Cheers
April 26, 2007 at 5:24 pm
arhg – I have tried this, I have tried swapDepths to all sorts of numbers
I can check the depths of each of my movieClips on screen (stored in an array)
but if I want to remove them all I can’t remove the first or last in the array.
I have tried added an item to the start and end of the array to sort the problem – but it still doesn’t work.
My depths with problems are 1 and 5! Go figure
They have also been created in exactly the same way as the other movieClips made. (this.createEmptyMovieClip etc)
But by tracing AFTER it removeMovieClip() has been called – the depth is now set to -32769. and I’ve tried calling your function a second time – still doesn’t work.
I hang my head in shame
Please – I know this was posted a long time ago, but if someone can work it out I love it for you to share the secret with me. (F8, PC)
April 26, 2007 at 7:12 pm
Hi Boris,
I would like to help you but I’m flabbergasted by this weird Flash behavior.
I assume Flash is to blame because it sure sounds inconsequent that a part of the MovieClips is successfully removed while the rest isn’t.
I’m not aware of any secret spell to cure this madness.
A quick and dirty solution that comes to my mind is to set the MovieClip’s visibility property (_visible) to false and/or place it outside the visible Stage before removing it, to assure the MovieClips at least won’t bother anymore.
If you want a better informed helping attempt then please comment or email (theo.lagendijk < at > gmail.com) the source code that creates and destroys the MovieClips, because I might spot something you overlooked.
June 4, 2007 at 10:12 am
[...] Removing DisplayObjects works by calling an DisplayObjectContainer’s removeChild function. I’m so excited about this! New Flash users might not see the significance of this, but you would appreciate this as much as I do I you knew what a hassle it occasionally was to remove MovieClips from the Stage in ActionScript 2.0. [...]
June 19, 2007 at 8:45 pm
“@Dragy: I don’t know why the Flash developers choose this seemingly random range of depth numbers.
I accepted it as a fact. In my opinion there’s far to much nonsense in Flash and Actionscript to waste my energy uncovering the motives. But if you happen to figure it out, please let me know!”
Between ‘0′ and ‘1048575′ there are 1048576 different levels. 1048576 is 2^20 (2 to the 20th power) and is the range that flash’s data structures allow.
August 5, 2007 at 11:18 am
there’s little trick:
suppose you have ‘container_mc’ and ‘inside_mc’ that is created in container_mc…
now:
container_mc.createEmptyMovieClip(“remover”, inside_mc.getDepth());
trick is to create another movie clip on the same depth that movieclip that we want to remove occupies.
now, that newly create movie clip (‘remover’) is easy to remove with removeMovieClip()
I don’t know why but this realy works, mostly…
I mean, i have problems with dynamicaly created components, like ComboBox, Button etc…
still trying to figure it out, anyway…
September 27, 2007 at 2:12 pm
thanks!!!
October 4, 2007 at 6:32 pm
It helped!! Thanks a lot!
October 10, 2007 at 12:44 pm
tiny script, huge benefits!!!
thanks man
April 16, 2008 at 8:42 am
Awesome ! Thanks a lot!
July 24, 2008 at 2:48 pm
Still saving lifes, man!
November 6, 2008 at 5:31 pm
Thank you so much Theo
November 6, 2008 at 6:02 pm
No problem! Glad you all appreciate it.
December 25, 2008 at 8:35 am
Awesome… I think the object I made was on a depth somewhere between 1 and 3… Yet… It would not remove…
//Get rid of this title once user clicks!!!
YOUCLICKNOW.onMouseDown = function(){
removeMC(YOUCLICKNOW);
trace(“WHOOOOOOO”);
}//End function
But… With your function… I was able to make a movie clip remove itself… Awesome.
March 4, 2009 at 7:26 pm
Just wanted you to know you are still saving lives, or in my case A’s
My final project appreciates your help!
Thanks Much!
March 4, 2009 at 8:09 pm
You’re welcome!
March 8, 2009 at 12:11 am
Yet another gratefull user.
I’m so much appreciate your help as much I’m angry toward the flash guys. They offer to us the function getNextHighestDepth() that is essentialy for attaching movie dynamicly, for example:
_root.attachMovie( “movieFromLibrary”, “NewName”, getNextHighestDepth() );
But the value that getNextHighestDepth() returns is 1048578 rigth from the first call. So all the movies that created such way are gonna be at the level above this one. How the hell they want us to remove it after that?
March 8, 2009 at 8:49 am
Hi aliowka,
I feel your anger. That’s indeed just cruel! I’m glad things have gotten better in AS3.
June 10, 2009 at 9:44 am
Hey, I used the attachMovie function to attach several instances of a movieClip to my flash document (Flash MX 2004), but there is an instance of the original clip on the screen. Each movieClip is attached with a different name. I am trying to remove the original one, but it does not work. I even tried your code above. Do you know what is the problem?
code: _root.attachMovie(“pointsButton”,”btn”+i,_root.getNextHighestDepth());
pointsButton is the original movieClip, and I want only the new instances to be attached.
Any help?
Thanks
June 11, 2009 at 8:36 am
Hi Fitz,
I hope you’ve found an answer to your question. If my custom removeMC function does not work for you then I don’t think I’m able to help you. (Are you passing the right movieclip as the parameter for the function?) It’s been a long time since I’ve worked with ActionScript 2.0. I’m now developing Flash apps in the much more mature ActionScript 3.0 programming language (using the Flex 3 open source SDK). This really is a world apart from ActionScript 2.0 and I’ve long forgotten the headaches of developing with Flash apps ActionScript 2.0.
Good luck!
November 24, 2009 at 11:58 am
Thanks a lot man, super lifesaving script
Glad we have people like you ;D
November 24, 2009 at 12:06 pm
Hi Aoshin,
I’m glad I saved a life today!