检查键盘状态,而在AS3中使用的KeyboardEvent而在、键盘、状态、KeyboardEvent

2023-09-08 14:13:43 作者:{未曾㊣糹坙}

是否有可能来检查pressed键,而无需使用的KeyboardEvent?

Is it possible to check for pressed keys without using the KeyboardEvent?

我有一个ENTER_FRAME事件设置称为enterFrameHandler和我想要的功能enterFrameHandler内检查是否有键pressed。

I have an ENTER_FRAME event setup called enterFrameHandler and I want to check within the function enterFrameHandler if any keys are pressed.

通常,当我可以检查钥匙轻松地使用检查事件的关键code开关,但在ENTER_FRAME事件,这是不可能的我。

normally when using a KeyboardEvent I could check for keys easily using a switch that checks the KeyCode of the event, but in an ENTER_FRAME event this isn't possible for me.

有ENTER_FRAME事件中检查键盘的状态下的任何其他方式?

Is there any other way of checking the keyboard's state within the ENTER_FRAME event?

更新: 我发现这个AS2脚本:

UPDATE: I found this AS2 script:

onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT)) {
        _x -= power;
    }
    if (Key.isDown(Key.RIGHT)) {
        _x += power;
    }
    if (Key.isDown(Key.UP)) {
        _y -=power;
    }
    if (Key.isDown(Key.DOWN)) {
        _y +=power;
    }
}

这似乎是在做什么,我想要的,但它在AS2,没有人知道如何'翻译'到这一点AS3?

This seems to be doing what I want, but it's in AS2, does anyone know how to 'translate' this into AS3?

推荐答案

在字典或对象存储关键州:

Store key states in a dictionary or object:

stage.addEventListener(KeyboardEvent.KEY_UP, keyHandleUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandleDown);

private var hash:Object = {};

private function keyHandleUp(event:KeyboardEvent):void {
    delete hash[event.keyCode];
}

private function keyHandleDown(event:KeyboardEvent):void {
    hash[event.keyCode] = 1;
}

private function isKeyDown(code:int):Boolean {
    return hash[code] !== undefined;
}
 
精彩推荐
图片推荐