简体   繁体   English

AS3中的多个按键条件

[英]Multiple key press conditionals in AS3

I'm creating a game that requires the simultaneous pressing of keys as a confirmation. 我正在创建一个需要同时按下按键作为确认的游戏。

Currently, I am attempting to use charCode with && conditionals in an if statement: 目前,我正在尝试在if语句中将charCode与&&条件一起使用:

function reportKeyDown(event:KeyboardEvent):void 
{ 
    if (event.charCode == 97 && 109 && 13)
    {
     gotoAndStop(30) 
    }
    else {
        gotoAndStop(20)
        //subtract 5hp
    }
} 
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);

So far it will only recognize the first key. 到目前为止,它只会识别第一个密钥。 Any ideas on how to solve this? 关于如何解决这个问题的任何想法?

One possible solution is to save keyCodes in an array and check which keys was pressed after a short time 一种可能的解决方案是将keyCodes保存在数组中,并检查短时间后按下了哪些键

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
var timer:Timer=new Timer(100);
var tmpKeys:Array=[];
timer.addEventListener(TimerEvent.TIMER,reportKeyDown);
function keyDownHandler(e:KeyboardEvent):void{ 
    trace(e.keyCode);
    tmpKeys.push(e.keyCode);
    timer.start();
} 
function reportKeyDown(e:TimerEvent):void{
    if(keysAreDown([Keyboard.N,Keyboard.P])) //check for simultaneous pressing of 'n' and 'p'
        trace("yesss!");
    else
        trace("nooo!");
    tmpKeys=[];
    timer.stop();
}
function keysAreDown(keys:Array):Boolean{
    for each(var key:int in keys)
        if(tmpKeys.indexOf(key)==-1)
            return false;
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM