View comments | RSS feed

removeMovieClip (MovieClip.removeMovieClip method)

public removeMovieClip() : Void

Removes a movie clip instance created with duplicateMovieClip(), MovieClip.duplicateMovieClip(), MovieClip.createEmptyMovieClip(), or MovieClip.attachMovie().

This method does not remove a movie clip assigned to a negative depth value. Movie clips created in the authoring tool are assigned negative depth values by default. To remove a movie clip that is assigned to a negative depth value, first use the MovieClip.swapDepths() method to move the movie clip to a positive depth value.

Note: If you are using version 2 components, do not use this method. If you place a version 2 component either on the Stage or in the Library, the getNextHighestDepth() method can sometimes return depth 1048676, which is outside the valid range. If you are using version 2 components, you should always use the version 2 components DepthManager class.

Note: If you are using version 2 components, and use MovieClip.getNextHighestDepth() instead of the version 2 components DepthManager class to assign depth values, you may find that removeMovieClip() fails silently. When any version 2 component is used, the DepthManager class automatically reserves the highest (1048575) and lowest (-16383) available depths for cursors and tooltips. A subsequent call to getNextHighestDepth() returns 1048576, which is outside the valid range. The removeMovieClip() method fails silently if it encounters a depth value outside the valid range. If you must use getNextHighestDepth() with version 2 components, you can use swapDepths() to assign a valid depth value or use MovieClip.unloadMovie() to remove the contents of the movie clip. Alternatively, you can use the DepthManager class to assign depth values within the valid range.

You can extend the methods and event handlers of the MovieClip class by creating a subclass.

Availability: ActionScript 1.0; Flash Player 5

Example

Each time you click a button in the following example, you attach a movie clip instance to the Stage in a random position. When you click a movie clip instance, you remove that instance from the SWF file.

function randRange(min:Number, max:Number):Number {
    var randNum:Number = Math.round(Math.random()*(max-min))+min;
    return randNum;
}
var bugNum:Number = 0;
addBug_btn.onRelease = addBug;
function addBug() {
    var thisBug:MovieClip = this._parent.attachMovie("bug_id", "bug"+bugNum+"_mc", bugNum, 
{_x:randRange(50, 500), _y:randRange(50, 350)});
    thisBug.onRelease = function() {
    this.removeMovieClip();
    };
    bugNum++;
}

See also

duplicateMovieClip function, createEmptyMovieClip (MovieClip.createEmptyMovieClip method), duplicateMovieClip (MovieClip.duplicateMovieClip method), attachMovie (MovieClip.attachMovie method), swapDepths (MovieClip.swapDepths method)


Version 8

Comments


graphXdziner said on May 20, 2006 at 6:16 PM :
Here's a nifty solution to the depth problem.

function safelyRemoveClip( mc:MovieClip ):Void
{
var mcTemp:MovieClip = mc._parent.getInstanceAtDepth(0);
mc.swapDepths(0);
mc.removeMovieClip();
if (mcTemp != undefined) mcTemp.swapDepths(0);
}
safelyRemoveClip ( myClip );
wbeartop said on Oct 25, 2006 at 5:02 AM :
How do you create a sub class of remove clip so as to directly remove
a depth level. Is the assignment of (""+i),i) in a for loop similar to setting the (""+i),i)to be an object. It seems simple enough to reference and remove after use of Eval any depth level. Removal of Eval in Action script
3 would present major problems for me.
No screen name said on Dec 17, 2006 at 6:01 PM :
Except this doesn't seem to actually remove all reference to a movieclip:

I have been having problems with depth issues because I use a temporary movieclip to swap multiple movie instances which is created with the same name - even when it is removed - the clip is still listed as a child:

E.g. swap 'myMovie' to the top of the level stack in main timeline:

_root.createEmptyMovieClip("placeHolder", _root.getNextHighestDepth());
_root.myMovie.swapDepths(_root.placeHolder);
_root.placeHolder.removeMovieClip();

Now check children of the _root:

for (var i in _root)
{
if (typeof (this[i]) == "movieclip")
{
trace("name: " + this[i]._name + ",\t target:" + eval(this[i]._target));
}
}

And low and behold - 'placeHolder' will still be there - if you happen to put anything else on the level this placeHolder occupies - you're in trouble even if the clip was removed!

I have even tried:

var tempClip:MovieClip = _root.createEmptyMovieClip("placeHolder", _root.getNextHighestDepth());
myMovie.swapDepths(tempClip);
tempClip.placeHolder.removeMovieClip();
delete tempClip;

Frankly, I find the way depth issues are handled very counter productive to OO - not to mention memory leaks?!

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00002510.html