2016-10-13 00:16:48 点击量:7 标签: 收藏本文
[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);
- }
- }
- }
我们是一家提供综合软件外包与弱电工程服务的技术公司。
业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。
公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室