[AS3]as3下侦听键盘动作和鼠标动作的源代码示例
[AS3]as3下侦听键盘动作和鼠标动作的源代码示例
[AS3]as3下侦听键盘动作和鼠标动作的源代码示例
- package
- {
- import flash.display.Stage;
- import flash.events.KeyboardEvent;
- import flash.ui.Keyboard;
- import flash.events.MouseEvent;
- /**
- * CuPlayer.com处理玩家输入类---侦听键盘和鼠标
- */
- 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)
- {
- //CuPlayer.com侦听玩家键盘和鼠标事件
- 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;
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...