Home arrow Flash arrow Referencing Movie Clips in Flash MX

Referencing Movie Clips in Flash MX

In this third part of a four-part series, you will learn how to store references to clips in data containers, refer to nested instances, 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: 4 stars4 stars4 stars4 stars4 stars / 9
February 08, 2007
  1. · Referencing Movie Clips in Flash MX
  2. · Referring to Nested Instances
  3. · Authoring Instance References with Insert Target Path
  4. · Storing references to clips in data containers

print this article

Referencing Movie Clips in Flash MX
(Page 1 of 4 )

Referring to the Current Instance or Movie

We don’t always have to use an instance’s name when referring to a clip. Code attached to a frame in an instance’s timeline can refer to that instance’s properties and methods directly, without any instance name.

For example, to set the_alphaproperty of a clip namedcloud_mc, we can place the following code on a frame in thecloud_mctimeline:

  _alpha = 60;

Similarly, to invoke the play() method oncloud_mcfrom a frame in thecloud_mctimeline, we can simply use:


This technique can be used on any timeline, including timelines of main movies. For example, the following two statements are synonymous if attached to a frame on the main timeline of a Flash document. The first refers to the main movie implicitly, whereas the second refers to the main movie explicitly via the global_root property:


However, not all methods can be used with an implicit reference to a movie clip. Any movie clip method that has the same name as a corresponding global function, such as duplicateMovieClip() or unloadMovie(), must be invoked with an explicit instance reference. Hence, when in doubt, use an explicit reference. We’ll have more to say about method and global function conflicts later in in this chapter under “Method Versus Global Function Overlap Issues.”

Note that it’s always safest to use explicit references to variables or movie clips rather than using implicit references. Implicit references are ambiguous, often causing unexpected results and confusing other developers reading your code.

Self-references with the this keyword

When we want to refer explicitly to the current instance from a frame in its timeline or from one of its event handlers, we can use the this keyword. For example, the following statements are synonymous when attached to a frame in the timeline of our cloud_mcinstance:

  _alpha = 60;        // Implicit reference to the current timeline
  this._alpha = 60;   // Explicit reference to the current timeline

There are three reasons to usethisto refer to a clip even when we could legitimately refer to the clip’s properties and methods directly.

  • First, explicit references are easier for other developers to read, because they make the intention of a statement unambiguous.
  • Second, when used without an explicit instance reference, certain movie clip methods are mistaken for global functions by the interpreter. If we omit thethisreference, the interpreter thinks we’re trying to invoke the analogous global function and complains that we’re missing the target movie clip parameter. To work around the problem, we usethis, as follows:

      this.duplicateMovieClip("newClouds_mc", 0); // Invoke a method on an instance

      // If we omit the this reference, we get an error
      duplicateMovieClip("newClouds_mc", 0);      // Oops! 
  • Third, usingthis, we can conveniently pass a reference to the current timeline to functions that operate on movie clips:

      // Here's a function that manipulates clips
    function moveClipTo (theClip, x, y) {
    theClip._x = x;
    theClip._y = y;

    // Now let's invoke it on the current timeline
    moveClipTo(this, 150, 125);

    New and experienced object-oriented programmers alike, take note: the meaning ofthisinside a method is a reference not to the current timeline but to the object through which the method was invoked. If an object’s method needs to refer to a specific movie clip, a reference to that clip should be passed to the method as a parameter. See Chapter 12 for details on using the thiskeyword inside methods.

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