570
Creating Interaction with ActionScript
To decide which keys to use and determine their virtual key codes, use one of the following
approaches:
■
See the list of key codes in
Appendix C, “Keyboard Keys and Key Code Values.”
■
Use a Key class constant. (In the Actions toolbox, click ActionScript 2.0 Classes > Movie >
Key > Constants.)
■
Assign the following
onClipEvent()
handler to a movie clip, and select Control > Test
Movie and press the desired key:
onClipEvent(keyDown) {
trace(Key.getCode());
}
The key code of the desired key appears in the Output panel.
A common place to use Key class methods is within an event handler. In the following
example, the user moves the car using the arrow keys. The
Key.isDown()
method indicates
whether the key being pressed is the right, left, up, or down arrow. The event listener,
Key.onKeyDown
, determines the
Key.isDown(
keyCode
)
value from the
if
statements.
Depending on the value, the handler instructs Flash Player to update the position of the car
and to show the direction.
The following example shows how to capture keypresses to move a movie clip up, down, left,
or right on the Stage, depending on which corresponding arrow key (up, down, left, or right)
is pressed. Also, a text field shows the name of the pressed key.
To create a keyboard-activated movie clip:
1.
On the Stage, create a movie clip that can move in response to keyboard arrow activity.
In this example, the movie clip instance name is
car_mc
.
2.
Select Frame 1 in the Timeline; then select Window > Actions to open the Actions panel
if it is not already visible.
3.
To set how far the car moves across the screen with each keypress, define a
distance
variable and set its value to 10:
var distance:Number = 10;
4.
Add the following ActionScript code to the Actions panel below the existing code:
this.createTextField("display_txt", 999, 0, 0, 100, 20);
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:
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
};
Key.addListener(keyListener);
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...