380
Working with Movie Clips
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 378
). The difference is a new
property named
distance
, whose value determines how many pixels a movie clip moves each
time it is clicked.
To pass arguments to a custom class:
1.
Create a new ActionScript document and save it as
MoveRightDistance.as
.
2.
Type the following ActionScript into the Script window:
// MoveRightDistance class -- moves clip to the right 5 pixels every
frame.
class MoveRightDistance extends MovieClip {
// Distance property determines how many
// pixels to move clip for each mouse press.
var distance:Number;
function onPress() {
this._x += this.distance;
}
}
3.
Save your progress.
4.
Create a new Flash document, and save it as
MoveRightDistance.fla
in the same directory
as the class file.
5.
Create a movie clip symbol that contains a vector shape, such as an oval, and then delete
any content from the Stage.
You only need a movie clip symbol in the library for this example.
6.
In the Library panel, right-click (Windows) or Control-click (Macintosh) the symbol and
select Linkage from the context menu.
7.
Assign the linkage identifier
Ball
to the symbol.
8.
Type
MoveRightDistance
into the AS 2.0 Class text box.
9.
Add the following code to Frame 1 of the Timeline:
this.attachMovie("Ball", "ball50_mc", 10, {distance:50});
this.attachMovie("Ball", "ball125_mc", 20, {distance:125});
This code creates two new instances of the symbol on the root timeline of the SWF file.
The first instance, named
ball50_mc
, moves 50 pixels each time it is clicked; the second,
named
ball125_mc
, moves 125 pixels each time it is clicked.
10.
Select Control > Test Movie to test the SWF file.
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...