13.6 Removing Clip Instances and Main Movies

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 13.  Movie Clips

We've seen how to create and refer to movie clips; now let's see how to turn them into so many recycled electrons (in other words, blow 'em away).

The manner in which we create an instance or a movie determines the technique we use to remove that instance or movie later. We can remove movies and instances explicitly using unloadMovie( ) and removeMovieClip( ). Additionally, we can evict a clip implicitly by using loadMovie( ), attachMovie( ), or duplicateMovieClip( ) to place a new clip in its stead. Let's look at these techniques individually.

13.6.1 Using unloadMovie( ) with Instances and Levels

The built-in unloadMovie( ) function can remove any clip instance or main movie both those created manually and those created via loadMovie( ), duplicateMovieClip( ), and attachMovie( ). It can be invoked either as a global function or as a instance-level method:

unloadMovie(clipOrLevel);   // Global function clipOrLevel.unloadMovie( );  // Method

In the global function form of unloadMovie( ), clipOrLevel is a string indicating the path to the clip or level to unload. Because movie clips are converted to paths when used as strings, clipOrLevel can also be a movie clip reference. In the method form of unloadMovie( ), clipOrLevel must be a reference to a movie clip object. The exact behavior of unloadMovie( ) varies according to whether it is used on a level or an instance.

13.6.1.1 Using unloadMovie( ) with levels

When applied to a level in the document stack (e.g., _level0, _level1, or _level2), unloadMovie( ) completely removes the target level and the movie that the level contains. Subsequent references to the removed level yield undefined. Removing document levels is the most common use of the unloadMovie( ) function:

unloadMovie("_level1"); _level1.unloadMovie();
13.6.1.2 Using unloadMovie( ) with instances

When applied to an instance (whether the instance is manually or programmatically created), unloadMovie( ) removes the contents of the clip, but it does not remove the clip itself ! The timeline and canvas of the clip are removed, but an empty shell remains on stage. That shell can be referenced until the instance is removed permanently via removeMovieClip( ) or until the span of frames on which the instance resides ends. Furthermore, any onClipEvent( ) handlers on the shell remain active.

This "partial" deletion of the instance presents an interesting possibility; it lets us maintain a generic container clip whose contents can be changed repeatedly via loadMovie( ) and unloadMovie( ). For example, we could use a single clip to load sections of a web site or images in a photo album. The following series of statements demonstrates the technique with an instance called clipA (though in a real application, these statements would include the appropriate preloader code):

clipA.loadMovie("section1.swf");  // Load a document into clipA clipA.unloadMovie( );              // Unload the document, leaving clipA intact clipA.loadMovie("section2.swf");  // Load another document into clipA

One note of caution with this approach: when used on an instance, unloadMovie( ) removes all custom properties of the clip contained by the instance. Physical properties such as _x and _alpha persist, but custom variables, functions, and event handler callbacks are lost.

If you use the global function form of unloadMovie( ) with a nonexistent clip or level instance as its argument, the clip from which you invoked the unloadMovie( ) function will, itself, unload.

For example, if _level1 is undefined, and we issue the following code from the main timeline of _level0, then _level0 will unload:

unloadMovie(_level1);

Yes, there's some logic to this behavior, but we'll cover that later in this chapter under Section 13.7. You can avoid the problem by using a string when specifying the clipOrLevel argument of unloadMovie( ) or by checking explicitly that clipOrLevel exists before unloading it. Here's an example of each approach:

unloadMovie("_level1");  // clipOrLevel specified as a string if (_level1) {           // Explicit check to make sure level exists   unloadMovie(_level1); }

13.6.2 Using removeMovieClip( ) to Delete Instances

To delete instances created via duplicateMovieClip( ), attachMovie( ) or createEmptyMovieClip( ), we can use removeMovieClip( ). We delete an instance when it is no longer needed in our application, such as when an enemy spaceship is destroyed or an alert window is closed. Note that removeMovieClip( ) works on duplicated or attached instances only. It cannot delete a manually created instance or a main movie. Like unloadMovie( ), removeMovieClip( ) can be used in both method and global function form (though the syntax is different, the effect is the same):

removeMovieClip(theClip)    // Global function theClip.removeMovieClip()   // Method

In the global function form of removeMovieClip( ), theClip is a string indicating the path to the clip to remove. Because movie clips are converted to paths when used as strings, theClip can also be a movie clip reference. In the method form of removeMovieClip( ), theClip must be a reference to a movie clip object.

Unlike using unloadMovie( ), deleting an instance via removeMovieClip( ) completely obliterates the entire clip object, leaving no shell or trace of the clip and its properties. When we execute theClip.removeMovieClip( ), future references to theClip yield undefined.

13.6.3 Removing Manually Created Instances Manually

Clip instances created manually in the Flash authoring tool have a limited life span they are removed when the playhead enters a keyframe that does not include them. Hence, manually created movie clips live in fear of the almighty blank keyframe.

Remember that when a movie clip disappears from the timeline, it ceases to exist as a data object. All variables, functions, methods, and properties that have been defined inside it are lost. Therefore, if we want a clip's information or functions to persist, we should be careful about removing the clip manually, and we should ensure that the span of frames on which the clip resides extends to the point where we need that clip's information. (In fact, to avoid this worry entirely, we should attach most permanent code to a frame in the main movie timeline or to the _global object.) To hide a clip while it's present on the timeline, simply set the clip's _visible property to false. Setting a clip's _x property to a very large positive number or very small negative number will also hide it from the user's view, but this approach is discouraged because the clip is still rendered off screen, consuming resources.

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net