Initializing class properties

In the example presented earlier, you added the instance of the Ball symbol to the Stage manually—that is, while authoring. As discussed previously (see Adding parameters to dynamically created movie clips), you can assign parameters to clips you create at runtime using the initObject parameter of attachMovie() and duplicateMovie(). You can use this feature to initialize properties of the class you're assigning to a movie clip.

For example, the following class named MoveRightDistance is a variation of the MoveRight class discussed earlier (see Assigning a class to a movie clip symbol). The difference is a new property named distance, whose value determines how many pixels a movie clip moves each time it is clicked.

// MoveRightDistance class -- moves clip to the right 5 pixels every frame
class MoveRightDistance extends MovieClip {
  // distance property determines how many
  // pixels to move clip each mouse press
  var distance:Number;
  function onPress() {
    this._x += distance;
  }
}

Assuming this class is assigned to a symbol with a linkage identifier of Ball, the following code creates two new instances of the symbol on the root Timeline of the SWF file. The first instance, named ball_50, moves 50 pixels each time it is clicked; the second, named ball_125, moves 125 pixels each time its clicked.

_root.attachMovie("Ball", "ball_50", 10, {distance:50});
_root.attachMovie("Ball", "ball_125", 20, {distance:125});