Referencing Movie Clips in Flash MX
(Page 1 of 4 )
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.
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:
play();
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:
gotoAndStop(20);
_root.gotoAndStop(20);
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, using
this, 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 of
thisinside 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.
Next: Referring to Nested Instances >>
More Flash Articles
More By O'Reilly Media
|
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.
|
|