Internet / Software Applications

Macromedia Flash MX 2004 ActionScript Programming Tutorial

Targets and Paths

In order to control the component parts of your movie, you need to understand how Flash expects you to refer to them. Instance names are used to refer to instances of a symbol on the stage. But Flash also needs to know where within the hierarchy of nested symbols the object is located. In Flash, you can specify an object’s relative path or its absolute path.

These terms should be familiar, but in short, an absolute path specifies the path from the root, while a relative path specifies a path from the current location. Instead of using slashes to indicate a hierarchy, as used on a web site (http://www.mysite.com/directorya/directoryb/file.htm) or on a computer hard drive (c:\directorya\directoryb\file.doc), Flash uses “dot” syntax—that is, you use dots, or periods, instead of slashes: _root.clipa.clipb.currentclip

The sky in the movie below is an instance of the “sky_clip” movie clip, which we’ve called “sky_mc”. The sky_clip movie clip is actually composed of animated cloud movie clips, the biggest of which is an instance of the “big_clouds_clip” movie clip called “bigclouds_mc”.

The “root” of a Flash movie is the main timeline, and so is aptly referred to using the “_root” keyword. Our sky_mc movie clip, residing on the main timeline, would be targeted using the absolute path:

_root.sky_mc

When using absolute paths, always begin with the root and work your way through the hierarchy of nested symbols to reach the one you’re targeting. From the main timeline, the target path to the bigclouds_mc movie clip instance would be:

_root.sky_mc.bigclouds_mc

If you’re referring to a symbol inside the current movie clip, it’s easier to use a relative path. The keyword “this” is used to refer to the current object—whatever object you’re adding the code to. For example, if we’re adding code to our sky_mc movie clip instance, we’d refer to the big_clouds_mc instance inside it using the relative path:

this.big_clouds_mc

You can also reference a movie clip one level up from the current one using the “_parent” keyword. From our big_clouds_mc movie clip, _parent would refer to the sky_mc movie clip. From there, you can use a relative path to target a clip within the parent’s hierarchy.

The “_global” keyword lets you indicate that an object is available to all the timelines in the movie. Variables and functions are sometimes assigned using the “_global” keyword.

To show you a simple example of targeting movie clips in the hierarchy, we’ll add code to the button “Click here to animate the clouds!”:

  1. The big_clouds_mc movie clip is a simple animation of clouds moving across the sky. To let the user control the animation, we first need to prevent it from playing automatically. To do so, double-click on the sky_mc movie clip to enter Edit mode. Then double-click on the big_clouds_mc movie clip instance to edit it. On the first frame of the Actions layer, we’ll add the stop() action:

Notice that the hierarchy is displayed above the timeline: first the scene, then the sky_clip movie clip, and then the big_clouds_clip movie clip. When you enter Edit mode, you edit the movie clips themselves. As you do so, Flash updates the instances of the movie clips on the stage.

  1. Exit Edit mode by clicking the Back button over the timeline, or by clicking the Scene 1 link next to it.
  2. In the main timeline, we’ll now select the “Click here” text, which we’ve converted to a button symbol. The instance is named “animate_clouds_btn”, which you can see on the tab at the bottom of the Script pane.
  3. In the Actions panel, we first need to add the on(release) event handler. We’ll do this by double-clicking the on action located in the Movie Clip Control folder in the Actions toolbox:

In the Intellisense menu, double-click release to insert the event into the handler:

  1. Next, we’re going to use the helpful Insert target path dialog to insert the path to our big_clouds_mc movie clip instance. With the cursor positioned inside the curly braces, click the Insert target path button near the top of the Actions panel:

The Insert Target Path dialog displays the symbol instances in the movie. When a symbol is nested inside another, as our big_clouds_mc movie clip instance is, it appears in its parent symbol. Expand the parent symbol to display it.

Notice that when you select a symbol instance, Flash displays the target path in the field above the list. If Absolute is selected at the bottom of the dialog, Flash displays the absolute path. If Relative (the default) is selected, Flash displays the relative path, beginning with “this”:

Because our button is on the main timeline, both paths are essentially the same.

  1. Select the target movie clip and click OK.
  2. Flash inserts the target path into the Script pane. Add a dot (.) at the end. When you do so, Flash interprets the target you added as an instance of the MovieClip object and displays a pop-up menu with all its available properties and methods:

Tip:

You can help Flash to recognize movie clip instances by adding “_mc” to the end of the names of the instances.

An object’s methods are the actions it can perform. In Flash, the MovieClip object has play, stop, and gotoAndPlay methods, among others, just like the simple actions you learned about earlier.

  1. Scroll through the menu and double-click the play method:

  1. Flash adds the code. Finish the line by adding a semicolon.

Now, when the user clicks the “Click here” text, the big_clouds_mc movie clip will start playing.

Flash gives you one final way to refer to an object’s location: using levels. Some actions have a “level” parameter, which simply refers to the level in the hierarchy you’re targeting, starting at the main timeline, level 0. Our sky_mc movie clip, residing on the main timeline, is located at level 1, and our big_clouds_mc, inside the sky_mc movie clip, is located at level 2.

Macromedia Flash MX 2004 ActionScript 2.0 Tutorial and Free Online Training Course

In this section, you learned about:

  • The Actions Panel
  • Customizing Flash
  • Syntax
  • Case Sensitivity
  • Dot Syntax
  • Functions and Event Handlers
  • Comments
  • Reserved Words
  • Simple Actions
  • Event Handling
  • Targets and Paths