Introduced
Demonstrates using Flash ActionScript 3.0来 to listen for user's keyboard events and mouse events, and to handle them accordingly
Keyboard events-under the Flash.events.KeyboardEvent class. There are only two event types Keyboardevent.key_down and keyboardevent.key_up.
Mouse events-under the Flash.events.MouseEvent class. Common event types are Mouseevent.mouse_down, Mouseevent.double_click, Mouseevent.mouse_up, Mouseevent.mouse_over, Mouseevent.mouse_move and Mouseevent.mouse_out and so on.
1. Handling Keyboard Events
Make 4 MovieClip with only two frames and put it on the UI, respectively, for the cue on/down/left/right keys, the first frame is displayed by default, and the second frame is displayed when the user presses the corresponding key. The names of these 4 MovieClip were Btnup, Btndown, Btnleft, Btnright
Then draw an object on the UI, as a result of displaying the user's keyboard events (up/down/left/right)
Main.as
Package
{
Import flash.events.Event;
Import flash.events.KeyboardEvent;
Import Flash.display.MovieClip;
Import Flash.ui.Keyboard;
Import flash.events.MouseEvent;
public class Main extends MovieClip
{
Save whether the user pressed the up/down/left/right key
public var Isup:boolean = false;
public var Isdown:boolean = false;
public var Isleft:boolean = false;
public var Isright:boolean = false;
The distance required to move each Enter_frame object
public var step:number = 5;
Public Function Main ()
{
Sets the initial state of the four MovieClip for display orientation to stop at the first frame
Btnup.stop ();
Btndown.stop ();
Btnleft.stop ();
Btnright.stop ();
Handle Press and release events for keystrokes (detect the event on the stage)
Stage.addeventlistener (Keyboardevent.key_down, Keypresshandler);
Stage.addeventlistener (keyboardevent.key_up, Keyreleasehandler);
Enter_frame for responding to key events
This.addeventlistener (Event.enter_frame, Enterframehandlerforkeyboard);
}
protected function Keypresshandler (event:keyboardevent): void
{
To detect the key pressed by the user
Switch (Event.keycode)
{
Case KEYBOARD.UP:
IsUp = true;
Btnup.gotoandstop (2);
block.rotation = 0;
Break
Case Keyboard.down://Press "Down" key
Isdown = true; Saves the user's key information. That is, the user is pressing the "Down" key
Btndown.gotoandstop (2); MovieClip to display the orientation "down" to the second frame
Block.rotation = 180; The object rotates 180 degrees.
Break
Case Keyboard.left:
Isleft = true;
Btnleft.gotoandstop (2);
Block.rotation = 270;
Break
Case Keyboard.right:
Isright = true;
Btnright.gotoandstop (2);
Block.rotation = 90;
Break
}
}
protected function Keyreleasehandler (event:keyboardevent): void
{
To detect user-released keys
Switch (Event.keycode)
{
Case KEYBOARD.UP://Press the "Up" button
IsUp = false; Saves the user's key information. That means the user is no longer pressing the "Up" button.
Btnup.gotoandstop (1); MovieClip to show the direction "up" to stop to the first frame
Break
Case Keyboard.down:
Isdown = false;
Btndown.gotoandstop (1);
Break
Case Keyboard.left:
Isleft = false;
Btnleft.gotoandstop (1);
Break
Case Keyboard.right:
Isright = false;
Btnright.gotoandstop (1);
Break
}
}
protected function Enterframehandlerforkeyboard (event:event): void
{
According to the user's key situation. Move the object up/down/left/right to the specified distance
if (isleft)
{
block.x-= step;
}
if (isright)
{
block.x + = step;
}
if (isUp)
{
BLOCK.Y-= step;
}
if (Isdown)
{
Block.y + = step;
}
}
}
}