Home arrow Flash arrow Page 3 - Referencing Movie Clips in Flash MX
FLASH

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Referencing Movie Clips in Flash MX - Authoring Instance References with Insert Target Path
(Page 3 of 4 )

When the instance structure of a movie gets very complicated, composing references to movie clips and main movies can be laborious. We may not always recall the exact hierarchy of a series of clips and, hence, may end up frequently selecting and editing clips in the authoring tool just to determine their nested structure. The Actions panel’s Insert Target Path tool generates clip references visually, relieving the burden of creating them manually. The Insert Target Path button is shown in Figure13-6.


Figure 13-6.  The Insert Target Path button

To use Insert Target Path, follow these steps:

  1. Position the cursor in your code where you want a clip reference to be inserted.
  2. Click the Insert Target Path button, shown in Figure13-6.
  3. In the Insert Target Path dialog box, select the clip to which you want to refer.
  4. Choose whether to insert an absolute reference, which begins with_root, or a relative reference, which expresses the reference to the target clip in relation to the clip that contains your code (this).
  5. If you are exporting to Flash 4 format, choose the Slashes Notation button for Flash 4 compatibility. (The Dot Notation button, selected by default, composes references that won’t work in Flash 4).

The Insert Target Path tool cannot generate relative references that ascend a hierarchy of clips. That is, the tool cannot be used to refer to a clip that contains the current clip (unless you want to begin the path from_rootand proceed downward). To create references that ascend the clip hierarchy, we must either use absolute references starting with_root(which therefore become descending references) or manually enter the appropriate relative references in our code using the_parentproperty.

Dynamic References to Clip Objects

Normally, we know the name of the specific instance or movie we are manipulating, but there are times when we’d like to control a clip whose name we don’t know. We may, for example, want to scale down a whole group of clips using a loop or create a button that refers to a different clip each time it is clicked. To handle these situations, we must create our clip references dynamically at runtime.

Using the array-element access operator

As we saw in Chapter 5 and Chapter 12, the properties of an object can be retrieved via the dot operator or through the array-element access operator, []. For example, the following two statements are equivalent:

  someObject.myProperty = 10;
  someObject["myProperty"] = 10;

The array-element access operator has one important feature that the dot operator does not; it lets us (indeed requires us to) refer to a property using a string expression rather than an identifier. For example, here’s a string concatenation expression that acts as a valid reference to the propertypropertyX:

  someObject["prop" + "ertyX"];

We can apply the same technique to create our instance and movie references dynamically. We already saw that clip instances are stored as properties of their parent clips. Earlier, we used the dot operator to refer to those instance properties. For example, from the main timeline we can refer toclipB—which is nested inside of another instance,clipA—as follows:

  clipA.clipB;           // Refer to clipB inside clipA
 
clipA.clipB.stop();    // Invoke a method on clipB

Because instances are properties, we can also legitimately refer to them with the[]operator, as in:

  clipA["clipB"];          // Refer to clipB inside clipA
  clipA ["clipB"].stop();  // Invoke a method on
clipB

Notice that when we use the[]operator to refer toclipB, we provide the name ofclipBas a string, not an identifier. That string reference can be any valid string-yielding expression. For example, here’s a reference toclipBthat involves a string concatenation:

  var clipCount = "B"; clipA["clip" + clipCount]; // Refer to clipB inside clipA clipA["clip" + clipCount].stop(); // Invoke a method on clipB

We can create clip references dynamically to refer to a series of sequentially named clips:

  // Here's a loop that stops clip1, clip2, clip3, and clip4
 
for (var i = 1; i <= 4; i++) {
    _root["clip" + i].stop();
  }

Now that’s powerful!


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 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials