侦听键盘和鼠标
package
{
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;
/**
* 处理玩家输入类---侦听键盘和鼠标
*/
public class UserInputHandler
{
public static var keyRight:Boolean;
public static var keyLeft:Boolean;
public static var keyForward:Boolean;
public static var keyBackward:Boolean;
public static var mouseDown:Boolean;
public function UserInputHandler(stage:Stage)
{
//侦听玩家键盘和鼠标事件
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
}
private function keyDownHandler(e:KeyboardEvent):void
{
switch(e.keyCode)
{
case "W".charCodeAt():
case Keyboard.UP:
UserInputHandler.keyForward = true;
UserInputHandler.keyBackward = false;
break;
case "S".charCodeAt():
case Keyboard.DOWN:
UserInputHandler.keyBackward = true;
UserInputHandler.keyForward = false;
break;
case "A".charCodeAt():
case Keyboard.LEFT:
UserInputHandler.keyLeft = true;
UserInputHandler.keyRight = false;
break;
case "D".charCodeAt():
case Keyboard.RIGHT:
UserInputHandler.keyRight = true;
UserInputHandler.keyLeft = false;
break;
}
}
private function keyUpHandler(e:KeyboardEvent):void
{
switch(e.keyCode)
{
case "W".charCodeAt():
case Keyboard.UP:
UserInputHandler.keyForward = false;
break;
case "S".charCodeAt():
case Keyboard.DOWN:
UserInputHandler.keyBackward = false;
break;
case "A".charCodeAt():
case Keyboard.LEFT:
UserInputHandler.keyLeft = false;
break;
case "D".charCodeAt():
case Keyboard.RIGHT:
UserInputHandler.keyRight = false;
break;
}
}
private function mouseDownHandler(e:MouseEvent):void
{
mouseDown = true;
}
private function mouseUpHandler(e:MouseEvent):void
{
mouseDown = false;
}
}
}