[AS3]AS3键盘事件的判断实现多个键同时按下的操作源代码
这个是方法一,有局限性。方法二是用数组把按下的键保存起来[AS3]AS3键盘事件的判断实现多个键同时按下的操作源代码
这个是方法一
- 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
- *@ 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 = "请按小键盘的上下左右或两个同时一起按,注意看着:" + String(curPosition);
- }
- }
- }
- package com{
- import flash.events.KeyboardEvent;
- import flash.events.Event;
- import flash.display.DisplayObject;
- import flash.utils.ByteArray;
- public class KeyPoll {
- private var states:ByteArray;
- private var dispObj:DisplayObject;
- public function KeyPoll( displayObj:DisplayObject ) {
- states = new ByteArray();
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- states.writeUnsignedInt( 0 );
- dispObj=displayObj;
- dispObj.addEventListener( KeyboardEvent.KEY_DOWN, keyDownListener, false, 0, true );
- dispObj.addEventListener( KeyboardEvent.KEY_UP, keyUpListener, false, 0, true );
- dispObj.addEventListener( Event.ACTIVATE, activateListener, false, 0, true );
- dispObj.addEventListener( Event.DEACTIVATE, deactivateListener, false, 0, true );
- }
- private function keyDownListener( ev:KeyboardEvent ):void {
- states[ ev.keyCode >>> 3 ] |= 1 << (ev.keyCode & 7);
- }
- private function keyUpListener( ev:KeyboardEvent ):void {
- states[ ev.keyCode >>> 3 ] &= ~(1 << (ev.keyCode & 7));
- }
- private function activateListener( ev:Event ):void {
- for (var i:int = 0; i < 32; ++i) {
- states[i]=0;
- }
- }
- private function deactivateListener( ev:Event ):void {
- for (var i:int = 0; i < 32; ++i) {
- states[i]=0;
- }
- }
- public function isDown( keyCode:uint ):Boolean {
- return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) != 0;
- }
- public function isUp( keyCode:uint ):Boolean {
- return ( states[ keyCode >>> 3 ] & (1 << (keyCode & 7)) ) == 0;
- }
- }
- }
方法二是用数组把按下的键保存起来
- package
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.events.KeyboardEvent;
- import flash.text.TextField;
- public class Main extends Sprite
- {
- var role:Sprite;
- var speed:int = 5;
- var keyObj:Object = {};
- var keyArr:Array = [];
- public function Main():void
- {
- role=new Sprite();
- role.graphics.beginFill(0xff00ff);
- role.graphics.drawRect(10,-40,20,40);
- role.graphics.endFill();
- role.x = 150;
- role.y = 200;
- addChild(role);
- stage.focus = this;
- keyOne();
- }
- private function keyOne()
- {
- stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDownHandler);
- stage.addEventListener(KeyboardEvent.KEY_UP,KeyUpHandler);
- }
- private function KeyDownHandler(e:KeyboardEvent)
- {
- keyObj[e.keyCode] = true;
- if (keyObj[32])
- {
- trace("space");
- //role.y -= 3 * speed;
- }
- if (keyObj[37])
- {
- trace("left");
- role.x -= speed;
- }
- if (keyObj[38])
- {
- trace("down");
- role.y -= speed;
- }
- if (keyObj[39])
- {
- trace("right");
- role.x += speed;
- }
- if (keyObj[40])
- {
- trace("up");
- role.y += speed;
- }
- if (keyObj[13] && keyObj[17])
- {
- trace("八连杀,好无敌。我要发射了。");
- var txt:TextField=new TextField();
- txt.multiline = true;
- txt.wordWrap = true;
- txt.text = "八连杀,好强大。一句就能爆菊花!";
- txt.x=int(50*Math.random());
- txt.y=-int(200*Math.random());
- while(role.numChildren>0)
- {
- role.removeChildAt(0);
- }
- role.addChild(txt);
- }
- }
- private function KeyUpHandler(e:KeyboardEvent)
- {
- keyObj[e.keyCode] = false;
- }
- private function keyTwo()
- {
- stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
- stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
- addEventListener(Event.ENTER_FRAME,enterFrameHandler);
- }
- private function keyDownHandler(e:KeyboardEvent)
- {
- var num = keyArr.indexOf(e.keyCode);
- if (num<0)
- {
- keyArr.push(e.keyCode);
- }
- }
- private function keyUpHandler(e:KeyboardEvent)
- {
- var num = keyArr.indexOf(e.keyCode);
- if (num>=0)
- {
- keyArr.splice(num,1);
- }
- }
- private function enterFrameHandler(e:Event)
- {
- trace(keyArr);
- }
- }
- }
热门文章推荐
- [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示例
请稍候...