[AS3]as3.0处理按键事件,多个按键按下时的响应事件,按键盘的事件
[AS3]as3.0处理按键事件,多个按键按下时的响应事件,按键盘的事件写法 package { importcom.KeyPoll; importflash.display.Sprite; importflash.events.Event; importflash.ui.Keyboard; importflash.text.TextField; importflash.text.TextFormat; [SWF( wid
[AS3]as3.0处理按键事件,多个按键按下时的响应事件,按键盘的事件写法
- package
- {
- import com.KeyPoll;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.ui.Keyboard;
- import flash.text.TextField;
- import flash.text.TextFormat;
- [SWF(width = "720",height = "450",frameRate = "12",backgroundColor = "0x3366ee")]
- /**
- *@ web: http://blog.vini123.com www.cuplayer.com
- *@ explanation 文章来自网上,整理vini。 这个对于键盘控制人物角色八个方向的行走还是比较方便的。
- */
- public class Main extends Sprite
- {
- private var key:KeyPoll;
- private var curPosition:int;
- private var txt:TextField;
- public function Main():void
- {
- addEventListener(Event.ADDED_TO_STAGE,addtoStageHandler);
- }
- private function addtoStageHandler(e:Event)
- {
- removeEventListener(Event.ADDED_TO_STAGE,addtoStageHandler);
- var txtFormat:TextFormat=new TextFormat();
- txtFormat.size = 14;
- txt=new TextField();
- txt.width=stage.stageWidth-20;
- txt.multiline=true;
- txt.setTextFormat(txtFormat);
- txt.textColor=0xff0000;
- txt.selectable = false;
- txt.mouseEnabled = false;
- txt.x = 10;
- txt.y = 20;
- addChild(txt);
- key = new KeyPoll(stage);
- addEventListener(Event.ENTER_FRAME,enterFrameHandler);
- }
- private function enterFrameHandler(e:Event)
- {
- controlKey();
- }
- private function controlKey():void
- {
- if (key.isDown(Keyboard.UP))
- {
- if (key.isDown(Keyboard.LEFT))
- {
- curPosition = 7;
- }
- else if (key.isDown(Keyboard.RIGHT))
- {
- curPosition = 8;
- }
- else
- {
- curPosition = 3;
- }
- }
- else if (key.isDown(Keyboard.DOWN))
- {
- if (key.isDown(Keyboard.LEFT))
- {
- curPosition = 5;
- }
- else if (key.isDown(Keyboard.RIGHT))
- {
- curPosition = 6;
- }
- else
- {
- curPosition = 1;
- }
- }
- else if (key.isDown(Keyboard.LEFT))
- {
- curPosition = 2;
- }
- else if (key.isDown(Keyboard.RIGHT))
- {
- curPosition = 4;
- }
- txt.text = "cuplayer.com提示请按小键盘的上下左右或两个同时一起按,注意看着:" + String(curPosition);
- }
- }
- }
热门文章推荐
- [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示例
请稍候...