Referencing Movie Clips in Flash MX - Storing references to clips in data containers
(Page 4 of 4 )
We began this chapter by saying that though movie clips are technically their own datatype, they are treated as objects in ActionScript. Hence, we can store a reference to a movie clip instance in a variable, an array element, or an object property.
Recall our earlier example of a nested instance hierarchy (clipCnested insideclipBnested insideclipA) placed on the main timeline of a document. If we store these various clips in data containers, we can control them dynamically using the containers instead of explicit references to the clips. Example 13-2, which shows code that is placed on a frame in the main timeline, uses data containers to store and control instances.
Example 13-2. Storing clip references in variables and arrays
var x = clipA.clipB; // Store a reference to clipB in the variable x
x.play(); // Play clipB
// Now let's store our clips in the elements of an array
var theClips = [clipA, clipA.clipB, clipA.clipB.clipC];
theClips[0].play(); // Play clipA theClips[1]._x = 200; // Place clipB 200 pixels from clipA's registration point
// Stop all the clips in our array using a loop
for (var i = 0; i < myClips.length; i++) {
myClips[i].stop();
}
By storing clip references in data containers, we can manipulate the clips (such as playing, rotating, or stopping them) without knowing or affecting the document’s clip hierarchy. Storing clip references in variables also make our code more legible. You can use a shorter, simpler variable name instead of a lengthy absolute or relative path through the movie clip hierarchy.
Using for-in to access movie clips
In Chapter 8, we saw how to enumerate an object’s properties using a for-in loop. Recall that a for-in loop’s iterator variable automatically cycles through all the properties of the object, so that the loop is executed once for each property:
for (var prop in someObject){
trace("the value of someObject." + prop + " is " + someObject[prop]);
}
Example 13-3 shows how to use a for-in loop to enumerate all the clips that reside on a given timeline.
Example 13-3. Finding movie clips on a timeline
for (var property in someClip) {
// Check if the current property of someClip is a movie clip
if (typeof someClip[property] == "movieclip") {
trace("Found instance: " + someClip[property]._name);
// Now do something to the clip
someClip[property]._x = 300;
someClip[property].play();
}
}
The for-in loop gives us convenient access to the clips contained by a specific clip instance or main movie. Using for-in, we can control any clip on any timeline, whether or not we know the clip’s name and whether the clip was created manually or programmatically.
Example 13-4 shows a recursive version of Example 13-3. It finds all the clip instances on a timeline, plus the clip instances on all nested timelines.
Example 13-4. Finding all movie clips on a timeline recursively
function findClips (theClip, indentSpaces) {
// Use spaces to indent the child clips on each successive tier
var indent = " ";
for (var i = 0; i < indentSpaces; i++) {
indent += " ";
}
for (var property in theClip) {
// Check if the current property of theClip is a movie clip
if (typeof theClip[property] == "movieclip") {
trace(indent + theClip[property]._name);
// Check if this clip is parent to any other clips
findClips(theClip[property], indentSpaces + 4);
}
}
}
findClips(_root, 0); // Find all clip instances descended from main timeline
For more information on recursion, see “Recursive Functions” in Chapter9.
The _name property
As we saw earlier in this chapter under “Movie Clip Instance Names,” every instance’s name is stored as a string in the built-in property _name. We can use that property, as we saw in Example 13-1, to determine the name of the current clip or the name of some other clip in an instance hierarchy:
this._name; // The current instance's name
_parent._name // The name of the clip that contains the current clip
The_nameproperty comes in handy when we want to perform conditional operations on clips according to their identities. For example, here we duplicate theseedClipclip when it loads:
onClipEvent (load) {
if (this._name == "seedClip") {
this.duplicateMovieClip("clipCopy", 0);
}
}
By checking explicitly for theseedClip name, we prevent infinite recursion—without our conditional statement, the load handler of each duplicated clip would cause the clip to duplicate itself.
The _target property
Every movie clip instance has a built-in _target property, which is a string that specifies the clip’s absolute path using the deprecated Flash 4 “slash” notation. For example, if clipB is placed inside clipA, andclipAis placed on the main timeline, the_targetproperty of these clips is as follows:
_root._target // Contains: "/"
_root.clipA._target // Contains: "/clipA"
_root.clipA.clipB._target // Contains: "/clipA/clipB"
The targetPath() function
The targetPath() function returns a string that contains the clip’s absolute reference path, expressed using dot notation. The targetPath() function is the modern, object-oriented equivalent of _target. It takes the form:
targetPath(theClip)
wheretheClip is the identifier of the clip whose absolute reference we wish to retrieve. Here are some examples, using our familiar example hierarchy:
targetPath(_root); // Contains: "_level0"
targetPath(_root.clipA); // Contains: "_level0.clipA"
targetPath(_root.clipA.clipB); // Contains: "_level0.clipA.clipB"
The targetPath() function gives us the complete path to a clip, whereas the_nameproperty gives us only the name of the clip. (This is analogous to having a complete file path versus just the filename.) So, we can use targetPath() to compose code that controls clips based not only on their name but also on their location. For example, we might create a generic navigational button that, by examining its targetPath(), sets its own color to match the section of content within which it resides. See the example under the Selection object in the Language Reference for a demonstration of targetPath() in action.
Please check back next week for the conclusion of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article 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). Check it out today at your favorite bookstore. Buy this book now.
|
|