如何检测as3中的多个按键按下事件? [英] How to detect multiple key down event in as3?

查看:24
本文介绍了如何检测as3中的多个按键按下事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习 AS3.

I just got started learning AS3.

假设我的精灵有两个文本字段.

Let's say I have two textfields on my sprite.

我喜欢在按向左或向右箭头键时移动文本字段 1,但我也想在文本字段 1 移动时按空格键移动文本字段 2,就像...正在移动).

I like to move textfield 1 when I press left or right arrow keys, but I also want to move textfield 2 when I press space while textfield 1 is moving like...an airplay game (you can shoot a missile while you're moving).

我真的很喜欢发布我的源代码......但我实际上不知道从哪里开始.

I really like to post my source code...but I actually have no idea where to begin.

当我按下方向键时,以下代码移动文本字段 1...

the following code moves textfield 1 when I press arrow keys...

我的代码片段:

private function keyHandler(event:KeyboardEvent):void
{

    switch(event.keyCode)
    {
        case 38:
            this._txt.y -= 10;
            break;
        case 40:
            this._txt.y += 10;
            break;

        case 39:
            this._txt.x += 10;
            break;
        case 37:
            this._txt.x -= 10;
            break;
    }


}

推荐答案

package  {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    /**
     * ...
     * @author www0z0k
     */
    public class KeyExample extends Sprite {
        private var _theyArePressed:Object = { };
        public function KeyExample() {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onUp);         
        }

        private function onUp(e:KeyboardEvent):void {
            _theyArePressed[e.keyCode] = false;
        }

        private function onDown(e:KeyboardEvent):void {
            _theyArePressed[e.keyCode] = true;
            if (_theyArePressed[Keyboard.SPACE] && _theyArePressed[Keyboard.UP]) {
                //do anything
            }
        }       
    }
}

但请记住,AFAIK 键盘可以同时处理有限数量的按键,具体取决于硬件

but keep in mind that that AFAIK keyboards can handle limited quantity of keys pressed at the same time, depending on the hardware

这篇关于如何检测as3中的多个按键按下事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆