Home arrow Flash arrow Fun Things to Do with Movie Clips in Flash MX

Fun Things to Do with Movie Clips in Flash MX

In this final part of a four-part series, you will learn how to remove clip instances, draw in a movie clip at run-time, and more. It is excerpted from chapter 13 of the book ActionScript for Flash MX: the Definitive Guide, second edition, written by Colin Moock (O'Reilly; ISBN: 059600396X). Copyright © 2005 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 3 stars3 stars3 stars3 stars3 stars / 23
February 15, 2007
  1. · Fun Things to Do with Movie Clips in Flash MX
  2. · Method Versus Global Function Overlap Issues
  3. · Drawing in a Movie Clip at Runtime
  4. · Using Movie Clips as Buttons
  5. · Input Focus and Movie Clips
  6. · Building a Clock with Clips

print this article

Fun Things to Do with Movie Clips in Flash MX
(Page 1 of 6 )

Removing Clip Instances and Main Movies

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.

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.

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 yieldundefined. Removing document levels is the most common use of the unloadMovie() function:


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 calledclipA (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

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_xand_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_level1is undefined, and we issue the following code from the main timeline of_level0, then_level0will unload:


Yes, there’s some logic to this behavior, but we’ll cover that later in this chapter under “Method Versus Global Function Overlap Issues.” You can avoid the problem by using a string when specifying theclipOrLevel argument of unloadMovie() or by checking explicitly thatclipOrLevel  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

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 totheClip  yieldundefined.

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_globalobject.) To hide a clip while it’s present on the timeline, simply set the clip’s_visibleproperty tofalse. Setting a clip’s_xproperty 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.

blog comments powered by Disqus

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials