Home arrow Flash arrow Page 2 - Fun Things to Do with Movie Clips in Flash MX
FLASH

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 / 22
February 15, 2007
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Fun Things to Do with Movie Clips in Flash MX - Method Versus Global Function Overlap Issues
(Page 2 of 6 )

As we’ve mentioned several times during this chapter, some movie clip methods have the same name as equivalent global functions. You can see this for yourself in the Flash authoring tool. Open the Actions panel, make sure you’re in Expert Mode, and then take a look in the Actions folder under Movie Control and Movie Clip Control. You’ll see a list of Actions, including gotoAndPlay(), gotoAndStop(), nextFrame(), and unloadMovie(). These Actions are also available as movie clip methods. The duplication is not purely a matter of categorization; the Actions are global functions, fully distinct from the corresponding movie clip methods.

So, when we execute:

  theClip.gotoAndPlay(5);

we’re accessing the movie clip method named gotoAndPlay(). But when we execute:

  gotoAndPlay(5);

we’re accessing the global function called gotoAndPlay(). These two commands have the same name, but they are not the same thing. The gotoAndPlay() global function operates on the current instance or movie. The gotoAndPlay() method operates on the clip object through which it is invoked. Most of the time, this subtle difference is of no consequence. But for some overlapping method/function pairs, this difference is potentially quite vexing.

Some global functions require atarget  parameter that specifies the clip on which the function should operate. Thistarget  parameter is not required by the comparable clip methods because the methods automatically operate on the clips through which they are invoked. For example, in its method form, unloadMovie() works like this:

  theClip.unloadMovie();

As a method, unloadMovie() is invoked without parameters, and it automatically affectstheClip. But in its global function form, unloadMovie() works like this:

  unloadMovie(target);

The global function version of unloadMovie() requirestarget as a parameter that specifies which movie to unload. Why should this be a problem? Well, the first reason is that we may mistakenly expect to be able to unload the current document by using the global version of unloadMovie() without any parameters, as we’d use gotoAndPlay() without parameters:

  unloadMovie();

This format does not unload the current clip. It causes a “Wrong number of parameters” error. The second reason thattarget  parameters in global functions can cause problems is a little more complex and can be quite a pain to track down if you’re not expecting it. To supply atarget  clip to a global function that requires atarget  parameter, we can use either a string, which expresses the path to the clip we wish to affect, or a clip reference. For example:

  unloadMovie(_level1);    // Target clip is a reference
  unloadMovie("_level1");  // Target clip is a string

We can use a reference simply because references to clip objects are converted to movie clip paths when used in a string context. This is simple enough, but if thetarget  parameter resolves to an empty string or anundefinedvalue, the function operates on the current timeline!

These examples demonstrate how an incorrect target clip reference can unintentionally unload the current timeline:

  unloadMovie(x);      // If x doesn't exist, x yields undefined, so
                       // the function operates on the current timeline
  unloadMovie("");     // The target is the empty string, so the function operates
                      
// on the current timeline

This can cause some quite unexpected results. Consider what happens if we refer to a level that doesn’t exist:

  unloadMovie(_level1);

If_level1is empty, the interpreter resolves the reference as though it were an undeclared variable. This yieldsundefined, so the function operates on the current timeline, not_level1! So, how do we accommodate this behavior? There are a few options. We can check for the existence of our target before executing a function on it:

  if (_level1) {
   
unloadMovie(_level1);
  }

Or, we can choose to always use a string to indicate the path to our target. If the path specified in our string does not resolve to a real clip, the function fails silently:

  unloadMovie("_level1");

In some cases, we can use the equivalent numeric function for our operation:

  unloadMovieNum(1);

Finally, we can choose to avoid the issue altogether by always using clip methods:

  _level1.unloadMovie();

For reference, here are the troublemakers (the ActionScript global functions that taketargetparameters):

  duplicateMovieClip()

  loadMovie()

  loadVariables()

  print( )

  printAsBitmap()

  removeMovieClip()

  startDrag()

  unloadMovie()

If you’re experiencing unexplained problems in a movie, you may want to check this list to see if you’re misusing a global function. When passing a clip reference as atarget parameter, be sure to double-check your syntax.


blog comments powered by Disqus
FLASH ARTICLES

- 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

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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