EasyManua.ls Logo

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT - Page 98

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...
98 Chapter 5: Creating Interaction with ActionScript
To create a keyboard-activated movie clip:
1 On the Stage, create a movie clip that will move in response to keyboard arrow activity.
In this example, the movie clip instance name is
car.
2 On the Stage, create a dynamic text box that will be updated with the direction of the car. Using
the Property inspector, give it an instance name of
display_txt.
Note: Don’t confuse variable names with instance names. For more information, see “About text
field instance and variable names” on page 136.
3 Select Frame 1 in the Timeline; then select Window > Development Panels > Actions to open
the Actions panel if it is not already visible.
4 To set how far the car moves across the screen with each keypress, define a distance variable
and set its initial value to 10.
var distance = 10;
5 To create the event handler for the car movie clip that checks which arrow key (left, right, up,
or down) is currently pressed, add the following code to the Actions panel:
car.onEnterFrame = function() {
}
6 Add a with statement to the body of the onEnterFrame handler, and specify car as the object
of the
with statement.
Your code should look like this:
var distance = 10;
car.onEnterFrame = function() {
with (car) {
}
}
7 To check if the Right Arrow key is being pressed, and to move the car movie clip accordingly,
add code to the body of the with statement. Your code should look like this:
distance = 10;
car.onEnterFrame = function() {
with (car) {
if (Key.isDown(Key.RIGHT)) {
_x += distance;
if (_x >= 400) {
_x = 400;
}
_root.display_txt.text = "Right";
}
}
}
If the Right Arrow key is down, the cars _x property is increased by the amount specified by
the
distance variable. The next if statement tests if the value of the clips _x property is
greater than or equal to 400 (if(_x >=400)); if so, its position is fixed at 400. Also, the word
Right should appear in the SWF file.

Table of Contents

Related product manuals