moock.org is supported in part by


June 28, 2007

Chapter 29, Pages 14-18, Essential ActionScript 3.0

Here are pages 14-18 of Chapter 29 of Essential ActionScript 3.0

29. ActionScript and the Flash Authoring Tool

In Chapter 1, we learned that the Flash authoring tool can be used to combine ActionScript code with graphics, animation, and multimedia assets. Now that we have a good understanding of the core ActionScript language, let’s explore the important links between ActionScript and content created in the Flash authoring tool.

...

Linked Classes for Movie Clip Symbols

From an ActionScript perspective, each Movie Clip symbol instance in a .fla file is represented at runtime by an instance of the Sprite class or one of its subclasses. The class used to represent instances of a specific Movie Clip symbol is known as that symbol’s linked class. A symbol’s linked class can be specified manually, or generated automatically.

To set the linked class for a Movie Clip symbol, we use the Linkage Properties dialog. Note that if any of the following are true, the specified linked class must inherit from flash.display.MovieClip:

* The symbol’s timeline contains any frame scripts.
* The linked class wishes to control instances of the symbol programmatically, using MovieClip methods.
* The symbol’s Stage contains any components with customized parameters and either of the following is true:
a) The customized parameters are not identical on all frames of the timeline. For example, a Button’s label is “OK” on frame 1 and “Submit” on frame 2.
b) The component does not appear on all frames of the timeline. For example, a List with a custom data provider appears on frame 1 but not on frame 2.
* The symbol’s Stage contains any components with customized accessibility properties or Strings Panel content.

Otherwise, the specified linked class need only inherit from flash.display.Sprite.

Here are the steps for specifying the linked class for a Movie Clip symbol.

1. Select the symbol in the .fla file’s Library.
2. Select the pop-up Options menu in the top-right corner of the Library panel, and choose the Linkage option.
3. In the Linkage Properties dialog box, for Linkage, select Export for ActionScript. Note that selecting Export for ActionScript forces the symbol to be included in the compiled .swf file, even if no instance of that symbol is used in the document.
4. In the Linkage Properties dialog box, for Class, enter the fully qualified class name (i.e., the class name combined with the class’s package, if the class resides in a package). The class being linked must be available in either the global classpath or the classpath of the .fla file containing the symbol. When specifying a linked class name in the Linkage Properties dialog, always leave the Base class field at its default value, except when linking more than one symbol to a single superclass, as discussed under “Linking Multiple Symbols to a Single Superclass.”
5. Click OK.

In step 4 of the preceding procedure, if the specified class is not found, the Flash compiler generates a linked class by the specified name automatically. The automatically generated class extends the specified base class.
If no linked class is specified for a given symbol, then the compiler assigns one automatically. If the following conditions are all met, then the automatically assigned linked-class is MovieClip:

* The symbol’s Stage does not contain any named instances.
* The symbol’s timeline has no frame scripts.
* The symbol’s Stage contains no components with customized parameters that vary across frames.
* The symbol’s Stage contains no components with customized accessibility properties or Strings Panel content.

Otherwise, the automatically assigned linked-class is an automatically generated MovieClip subclass.

Once a symbol is linked to a class, manually created instances of that symbol adopt the programmatic behaviors defined by the linked class. Conversely, programmatically created instances of the class adopt the audiovisual content of the linked symbol. Thus the symbol and the class are coupled together—the symbol defines the graphical content, while the class defines the programmatic behavior.

As an example, let’s link our star Movie Clip symbol from the preceding section to a MovieClip subclass, Star. The Star class will randomly change the transparency (alpha value) of each star instance every 100 milliseconds. Here’s the code:

package {
  import flash.display.MovieClip;
  import flash.utils.Timer;
  import flash.events.TimerEvent;
  public class Star extends MovieClip {
    private var timer:Timer;
    public function Star () {
      timer = new Timer(100, 0);
      timer.addEventListener(TimerEvent.TIMER, timerListener);
      timer.start();
    }
    private function timerListener (e:TimerEvent):void {
      randomFade();
    }
    private function randomFade ():void {
      // Set alpha to a random floating-point value from 0 up to (but
      // not including) 1. The instance variable alpha is inherited from
      // the DisplayObject class (which is ancestor of MovieClip).
      alpha = Math.random();
    }
    // Provide a means of stopping the timer. As discussed in section
    // "Deactivating Objects" in Chapter 14,
    // external code should use this method before removing a star instance
    // from the program.
    public function dispose ():void {
      timer.stop();
    }
  }
}

To link the Star class to the star Movie Clip symbol, we follow these steps:

1. Save the file containing the star symbol as sky.fla.
2. Save the Star class in a text file named Star.as, in the same folder as sky.fla.
3. Select the star symbol in sky.fla’s Library.
4. Select the pop-up Options menu in the top-right corner of the Library panel, and choose the Linkage option.
5. In the Linkage Properties dialog, for Linkage, select Export for ActionScript.
6. In the Linkage Properties dialog, for Class, enter Star.
7. Click OK.

To create instances of the star symbol in the Flash authoring tool, we drag the symbol name from the Library to the Stage of the main timeline (or to any other symbol’s Stage—but, in this case, there are no other symbols in the document).

Once the star symbol instances are on the main timeline, we can export sky.swf from sky.fla, and watch the each star’s twinkling motion (defined by the star symbol) and fading effect (defined by the Star class).

Now that we know how to manually create instances of Movie Clip symbols, let’s examine how to access and control them.

Posted by moock at June 28, 2007 09:42 PM