Targets and Paths Explained


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.

image from book
What Is Dot Syntax?

Flash 5 introduced a new method of writing all ActionScript called dot syntax. Earlier versions of Flash used a natural-language scripting environment that was menu-based, in which actions could be read and understood easily and accessed via pop-up menus and dialog boxes. While most people prefer easy-to-use scripting environments, the production demands of complex interactive projects are often compromised by such menu-driven scripting environments. Computer programmers prefer to create, edit, and debug scripting with a language they can access and modify easily. Consequently, we see the best of both worlds with Flash 8.

ActionScript adheres closely to the ECMA-262 specification, the same specification upon which JavaScript is based. JavaScript is the universal scripting language used by most browsers for Dynamic HTML (DHTML) documents. Therefore, Flash ActionScript uses a dot syntax, also known as dot notation. What does that mean? It means that all actions are written within a standard formula that is common with object-oriented programming (OOP) languages:

 Object.property = value; 

or

 Object.method(); 

The examples beg four things to be defined: objects, properties, methods, and values. An object is any element in a program (in this case, the Flash movie) that has changeable and accessible characteristics. Objects can be user-defined (in other words, you create and name them) or predefined by the programming language. Flash has several predefined objects, also called classes, meaning that they're already built into the ActionScript language. We look at object types in more detail in later chapters. An important object (and perhaps the easiest to conceptualize) is the MovieClip object. Any Movie Clip instance on the Stage is a MovieClip object, such as ballAnim_mc or dogTailAnim_mc. An object has characteristics, or properties, that can be updated or changed throughout the movie. An example of a MovieClip property is scale, which is referred to as _xscale and _yscale. We look at MovieClip properties in Chapter 25, "Controlling Movie Clips." Properties always have some data accompanying them. This data is called the property's value. Using the previous example, at full size, a MovieClip object's _xscale is 100 (the scale property uses percent as the unit of measure). For a MovieClip object named mcBallAnim, this would be represented in ActionScript syntax as:

 mcBallAnim._xscale = 100; 

Finally, objects can be enacted upon by procedures that do something to or with the object. These procedures are called methods. One method for the MovieClip object is the gotoAndPlay() method, which you used as a basic action in the previous chapter. In Flash Player 5 or higher movies, methods can be created for your own objects or predefined for existing Flash objects. Any goto action can be used as a method of any MovieClip object, as in:

 mcBallAnim.gotoAndPlay("start"); 

The preceding example tells the mcBallAnim instance to direct its playback head to the frame label start on its timeline. This chapter helps you understand how to use the gotoAndPlay() Method for Movie Clips.

image from book

Paths: Absolute and Relative Modes

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.

image from book
Paths in Flash 4 or Earlier Movies

Before Flash 5, the Main Timeline was represented in a Movie Clip path as a starting forward slash (/) character. The absolute path of a Movie Clip instance named mcDog on the Main Timeline is

 /mcDog 

Any nested Movie Clips inside of the mcDog instance would be referenced after that starting path. For example, the absolute path to mcTailAnim, an instance inside the mcDog Movie Clip instance, would be

 /mcDog/mcTailAnim 

Another / character was put between the two instance names. Think of the / as a substitute for the period or dot (.) in ActionScript target paths. Use of the / character in Movie Clip paths is known as the slash notation.

The equivalent to _parent in slash notation is a double-dot, as in

 ../ 

The two dots here work just like directory references for files on Web servers; use a pair of dots (..) for each timeline in the hierarchy.

Just as tellTarget is considered a deprecated action in Flash 8, the slash notation is deprecated syntax. It will still work with current Flash Players, but subsequent versions of the Flash authoring program will continue to be built upon dot notation.

image from book




Macromedia Flash 8 Bible
Macromedia Flash8 Bible
ISBN: 0471746762
EAN: 2147483647
Year: 2006
Pages: 395

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net