Assigning a class to a movie clip symbol
Using ActionScript 2.0, you can create your own class that extends the behavior of the built-in
MovieClip class, and then assign that class to a movie clip library symbol using the Linkage
Properties dialog box. Whenever you create an instance of the movie clip to which the class is
assigned, it assumes the properties and behaviors defined by the class assigned to it. (For more
information about ActionScript 2.0, see
on page
155.)
In a subclass of the MovieClip class, you can provide method definitions for the built-in
MovieClip methods and event handlers, like
procedure, you'll create a class called MoveRight that extends the MovieClip class; MoveRight
defines an
onPress
movie clip. In the second procedure, you'll create a movie clip symbol in a new Flash (FLA)
document and assign the MoveRight class to that symbol.
To create a movie clip subclass:
Create a new directory called BallTest.
1
Create a new ActionScript file by doing one of the following:
2
(Flash MX Professional 2004) Select File > New, and select ActionScript file from the list of
document types.
(Flash MX 2004) Create a text file in your preferred text editor.
Enter the following code in your script:
3
// MoveRight class -- moves clip to the right 5 pixels every frame
class MoveRight extends MovieClip {
function onPress() {
this._x += 20;
}
}
Save the document as MoveRight.as in the BallTest directory.
4
To assign the class to a movie clip symbol:
In Flash, select File > New, select Flash Document from the list of file types, and click OK.
1
Using the Oval tool, draw a circle on the Stage.
2
Select the circle, then select Modify > Convert to Symbol. In the Convert to Symbol dialog box,
3
select Movie Clip as the symbol's behavior and enter
Open the Library panel (Window > Library) and select the Ball symbol.
4
Select Linkage from the Library panel's options menu to open the Linkage Properties
5
dialog box.
In the Linkage Properties dialog box, select the Export for ActionScript option, and type
6
in the AS 2.0 Class text box. Click OK.
MoveRight
Save the file as Ball.fla in the BallTest directory (the same directory that contains the
7
MoveRight.as file).
Test the movie (Control > Test Movie).
8
Each time you click the ball movie clip, it moves 20 pixels to the right.
Chapter 9, "Creating Classes with ActionScript 2.0,"
handler that moves the clip 20 pixels to the right whenever the user clicks the
and
onEnterFrame
onRelease
in the Name text box.
Ball
Assigning a class to a movie clip symbol
. In the following
133
Need help?
Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?
Questions and answers