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 car’s
_x
property is increased by the amount specified by
the
distance
variable. The next
if
statement tests if the value of the clip’s
_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.
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...