User Guide

Initializing class properties 219
}
}
4.
Save the document as MoveRight.as in the BallTest directory.
To assign the class to a movie clip symbol:
1.
In Flash, select File > New, select Flash Document from the list of file types, and click OK.
2.
Using the Oval tool, draw a circle on the Stage.
3.
Select the circle, and select Modify > Convert to Symbol.
4.
In the Convert to Symbol dialog box, select Movie Clip as the symbol’s behavior, and enter
ball_mc in the Name text box.
5.
Select Advanced to show the options for Linkage, if they aren’t already showing.
6.
Select the Export for ActionScript option, and type MoveRight in the AS 2.0 Class text box.
Click OK.
7.
Save the file as ball.fla in the BallTest directory (the same directory that contains the
MoveRight.as file).
8.
Test the movie (Control > Test Movie).
Each time you click the ball movie clip, it moves 20 pixels to the right.
If you create component properties for a class and want a movie clip to inherit those component
properties, you need to take an additional step: with the movie clip symbol selected in the Library
panel, select Component Definition from the Library options menu and enter the new class name
in the AS 2.0 Class box. For more information, see “Creating Components” in Using Components.
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 in Adding parameters to dynamically created
movie clips” on page 213, 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 youre assigning to a movie clip.
For example, the following class named MoveRightDistance is a variation of the MoveRight class
(see Assigning a class to a movie clip symbol” on page 218). 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 += this.distance;
}
}