If you already studied Movie Clips in Chapter 6, "Symbols, Instances, and the Library," you probably know that they provide the solution to our animated dog problem. However, you might not have guessed that Movie Clips can also add logic to animation and Flash interfaces. Let's take our animated dog example a little further: When dogs bark, their tails may stop wagging. Our hypothetical dog may look strange if it is barking and wagging at the same time. Suppose we wanted to stop the tail wagging during every bark. We'd have to have some way for the barking head Movie Clip to control the tail Movie Clip so that we could tell the tail to stop wagging when the dog barks, and then tell the tail to return to its wagging loop again when the bark is over.
Well, you have a few ways to control the tail Movie Clip from the barking head Movie Clip. In Flash 3 and 4, the tellTarget action was used to let actions on any timeline (including Movie Clip timelines and the Main Timeline) control what happens on any other timeline. How? tellTarget simply provided a mechanism for extending basic actions such as play and stop, enabling them to specify (or target) the timeline upon which they should be executed. Targets are any Movie Clip instances that are available at the current "state" of a Flash movie — you can't target a Movie Clip that isn't displayed (or existing) on the Stage. For example, suppose you had two Movie Clips, one on frame 1 and another on frame 10 of the Main Timeline. If the Main Timeline was stopped on frame 1, you couldn't target the Movie Clip on frame 10 because the instance is not on the current frame.
Since Flash 5, developers have been able to direct actions to specific timelines by attaching the same actions as methods to a MovieClip object (we define methods in the following sidebar). As such, the tellTarget action is a deprecated action; it's still supported in current Flash Players, but it's been replaced with more versatile actions and syntax that makes its use outdated. For an overview of deprecated actions, see the sidebar on deprecated actions in the preceding chapter. In this chapter, you work exclusively with the preferred ActionScript dot syntax to control Movie Clip instances. First, however, you need to understand how targeting works in Flash movies.
Note |
If you're new to scripting, please read the "What Is Dot Syntax?" sidebar. |
Tip |
If you're building Flash movies for Flash Lite 1.0/1.1, you need to use the tellTarget action to control Movie Clip instances. Flash Lite 1.0/1.1 is based on the Flash Player 4 specification. |
Earlier in this chapter, you learned how multiple Movie Clip timelines appear on the Stage. It's entirely possible to nest several Movie Clips within another Movie Clip. To understand how Movie Clips communicate with one other by using actions, you need to have a firm grasp on Movie Clip paths. A path is simply that — the route to a destination, an address per se. If you have a Movie Clip instance named mcTailAnim inside a mcDog Movie Clip instance, how is Flash supposed to know? What if there was more than one mcTailAnim in the entire movie, with others nested in other Movie Clips besides the mcDog instance? You can specify a Movie Clip's path in an absolute or a relative mode.
An absolute path is the full location information, or target, for a given Movie Clip instance from any other location (or target). Just like your postal address has a street name and number and a ZIP code so that people can find you on a map, all Movie Clips have a point of origin: the Main Timeline (that is, Scene 1). Flash 8 only displays dot notation with absolute and relative paths.
Tip |
Since Flash MX 2004, the Insert Target Path dialog box no longer shows you dot and slash notations. See the sidebar "Paths in Flash 4 or Earlier Movies" for an explanation of slash notation. |
Note |
Dot notation and dot syntax are synonymous terms, and are used interchangeably throughout this book. |
Dot notation follows the ActionScript language conventions. With dot notation, the Main Timeline becomes
_root
A Movie Clip instance named mcDog on the Main Timeline (or _root) would have an absolute path of
_root.mcDog
Notice that a period, or dot, separates the term _root from mcDog. The dot denotes a parent-child relationship; the mcDog instance is a "child" of its parent, _root. And, following suit, a Movie Clip instance named mcTailAnim that is nested within the mcDog Movie Clip would have the absolute path of
_root.mcDog.mcTailAnim
A relative path is a contextual path to one timeline from another. From a conceptual point of view, think of a relative path as the relationship between the location of your pillow and the rest of your bed. Unless you have an odd sleeping habit, the pillow is located at the head of the bed. You may change the location of the bed within your room or the rooms of a house, but the relationship between the pillow and the bed remains the same. Another example that can illustrate the difference between absolute and relative references is the postal address example we used earlier. An absolute reference to your residence would use your full street address, city, state, and ZIP code. However, if you're giving directions to a friend of yours who lives nearby, you're more likely to tell your friend, "From your house, walk two blocks down A street, and turn right on B street. I'm five houses down on the left side of the street."
With Flash, relative Movie Clip paths are useful within Movie Clips that contain several nested Movie Clips. That way, you can move the container (or parent) Movie Clip from one timeline to another and expect the inner targeting of the nested Movie Clips to work. To refer to a timeline that is above the current timeline in dot notation, use
this._parent
Here, the term this refers to the current timeline from where the action is being called, and _parent refers to the current timeline's parent timeline. You can use relative dot notation to refer up and down the hierarchy at the same time. For example, if you have two nested Movie Clips, such as mcTailAnim and mcBarkingAnim, within a larger Movie Clip named mcDog, you may want to target mcTailAnim from mcBarkingAnim. The relative dot path for this task is
this._parent.mcTailAnim
This path tells Flash to go up one timeline from the current timeline, mcBarkingAnim, to its parent timeline (the mcDog timeline), and then look for the instance named mcTailAnim from there.
You can also use successive _parent references to travel up in the timeline hierarchy multiple times, such as
this._parent._parent
Using the mcDog instance example again, if you wanted to control the Main Timeline (which is the parent timeline of the mcDog instance) from the mcTailAnim instance, you could use _parent._parent in the target path of an action executed from the mcTailAnim timeline.
Note |
You can directly control the Main Timeline using the reference _root. However, as you'll see later in Chapter 28, "Sharing and Loading Assets," you may load an entire .swf file into a Movie Clip instance, thereby changing the reference to _root. Flash Player 7 and higher supports a property of MovieClip objects named _lockroot, which can help you avoid path problems when loading external .swf files. |
Web Resource |
Flash designers and developers learning to use ActionScript often encounter scope problems with their code. Scope refers to the code objects that can be "seen" within a given code block, such as a function. You can use the Delegate class to help you deal with issues of scope. Robert wrote a free tutorial on CommunityMX.com titled, "Better Practices for Flash Designers - Part 1: Coding Buttons." You can find a link to this article at www.flashsupport.com/cmx. This tutorial walks you through the use of the Delegate class with Flash buttons. The Delegate class can help you avoid using long chains of _parent references in your code. We also discuss the Delegate class in Chapter 33, "Using Components." |
As with absolute paths, we recommend that you become familiar with using the dot notation for relative paths.
Okay, that's enough theory. Now, you're going to practice nesting Movie Clips inside of other Movie Clips, as well as target actions at specific instances using dot notation.