What removeMovieClip() Becomes in AS3
       by senocular  |  11 January 2009

In ActionScript 2, movie clips had a very close association with the timeline in Flash. Once they were created within a timeline, that would be the only timeline in which they could exist until eventually removed, either by the timeline itself (based on frame information) or via ActionScript using removeMovieClip().

References to movie clips in AS2 were also soft references which used a target path to make a connection with a movie clip instance within its parent timeline. If a movie clip was removed from a timeline or non-existent at the time a variable referencing the movie clip was accessed, the variable would return no value because to access the variable it uses the target path of the movie clip to obtain a reference. With that target path no longer valid, the variable that would otherwise reference a movie clip instead references nothing (yet the variable will still be considered of the type movie clip). Look at the following example:

// ActionScript 2
var mcRef:MovieClip;
trace(mcRef); // undefined
trace(typeof mcRef); // undefined
mcRef = attachMovie("mySymbol", "mySymbol", 1);
trace(mcRef); // _level0.mySymbol
trace(typeof mcRef); // movieclip
trace(mcRef); // [results in an empty trace]
trace(typeof mcRef); // movieclip
attachMovie("mySymbol", "mySymbol", 2);
trace(mcRef); // _level0.mySymbol
trace(typeof mcRef); // movieclip

Even when the movie clip is removed from the screen, the mcRef variable which referenced it continued to reference a "movieclip" even though there was nothing to reference and the movie clip had been effectively destroyed in Flash. When a new movie clip is created in a different depth with the same instance name (hense the same target path) as the original, mcRef then points to that new movie clip as it has found a valid instance within the path it uses to lookup its respective movie clip value.

ActionScript 3 is less cryptic. Display objects are not so dependant on their timeline as before able to move from any timeline to another using addChild, removeChild, and similar methods. They are now also now created like all other objects using the new keyword (as opposed to attachMovie). And with this, variables that reference display object instances work the same way as with other instances meaning if you want to get rid of a display object instance in memory, you need to make sure there are no references to it.

What does this mean for a removeMovieClip replacement in AS3?

For one, you can remove a MovieClip instance (or other display object) from the screen quite easily using removeChild. If you are in the scope of the instance itself, you would use:


However, that will not remove the instance from memory, only the display list so it is not seen by the user. If you need the movie clip to be removed from memory, you will need to make sure there are no variables left that reference that movie clip. For class properties, null is used; for dynamic properties, delete.

classPropertyMcRef = null;
delete dynamicMcRef;

This represents a shift in responsibility of object removal from the object being removed to the parent or the timeline containing that object (or even the object that created it if not its parent).

In other words, movie clips should not be held accountable for whether or not they exist. This is the task of whatever object or parent is creating and using that movie clip. The object that created the instance would be (for the most part) the one controlling the references to that instance. By maintaining good encapsulation, it should do its best to prevent that reference to be spread too thinly across other class instances, especially if that movie clip needs to be removed at some point in time. This way that class can maintain all references, remove the instance from the display list when needed, and delete, nullify all references when it has to be removed from memory.

Similarly, you can minimize references or not use them altogether. By assigning unique instance names to movie clip instances (or other display objects) through their name property, you can easily dynamically obtain a reference when needed using getChildByName("instanceName"). By relying on this method to reference your movie clip instance instead of having other variable references, simply removing the instance from the display list with removeChild(getChildByName("instanceName")) will be all that is needed to remove the instance from memory.

If you have any questions, feel free to post on the kirupa.com Forums.





kirupa.com's fast and reliable hosting provided by Media Temple.