![MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT Reference Manual Download Page 133](http://html1.mh-extra.com/html/macromedia/flash-mx-2004-actionscript/flash-mx-2004-actionscript_reference-manual_3378897133.webp)
Assigning a class to a movie clip symbol
133
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
Chapter 9, “Creating Classes with ActionScript 2.0,”
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
onEnterFrame
and
onRelease
. In the following
procedure, you’ll create a class called MoveRight that extends the MovieClip class; MoveRight
defines an
onPress
handler that moves the clip 20 pixels to the right whenever the user clicks the
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:
1
Create a new directory called BallTest.
2
Create a new ActionScript file by doing one of the following:
■
(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.
3
Enter the following code in your script:
// MoveRight class -- moves clip to the right 5 pixels every frame
class MoveRight extends MovieClip {
function onPress() {
this._x += 20;
}
}
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, then select Modify > Convert to Symbol. In the Convert to Symbol dialog box,
select Movie Clip as the symbol’s behavior and enter
Ball
in the Name text box.
4
Open the Library panel (Window > Library) and select the Ball symbol.
5
Select Linkage from the Library panel’s options menu to open the Linkage Properties
dialog box.
6
In the Linkage Properties dialog box, 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.
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...