EasyManua.ls Logo

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT - Initializing Class Properties

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT
816 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
134 Chapter 7: Working with Movie Clips
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” on page 128), 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
discussed earlier (see Assigning a class to a movie clip symbol” on page 133). 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});

Table of Contents

Related product manuals