actionscript-3 – 如何在as3中检测多个按键事件?

我刚开始学习AS3.

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

当我按左箭头键或右箭头键时,我喜欢移动文本字段1,但是当我按下空格时我也想移动文本字段2而文本字段1移动就像…一个空中游戏(你可以在你移动时射击导弹) ).

我非常想发布我的源代码……但实际上我不知道从哪里开始.

当我按箭头键时,以下代码移动textfield 1 …

我的代码片段:

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键盘可以同时处理有限数量的按键,具体取决于硬件

https://stackoverflow.com/questions/7975668/how-to-detect-multiple-key-down-event-in-as3

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:actionscript-3 – 如何在as3中检测多个按键事件?