将 Enter 键链接到 Flash 游戏中的按钮

Linking Enter key to button in Flash game

我正在使用 actionscript 3.0 在 Adob​​e Flash 中构建拓扑训练器。 我现在快完成了。我在游戏结束时制作了这个 "quit" 按钮。 您现在可以单击它退出游戏,但我希望 Enter 键也能对退出按钮做出反应。我真的尝试在互联网和 Whosebug 上查找它,但要么我的搜索技术不够先进,要么还没有人遇到同样的问题。

我希望有人知道如何将 Enter 按钮耦合到 Flash 中的按钮。不涉及EditText。

感谢您的帮助,

贾斯汀

假设您有一些在游戏结束时运行的代码:

myQuitBtn.addEventListener(MouseEvent.CLICK, quit, false, 0, true);

function quit(e:MouseEvent):void {
    //do something, we quit the game
}

您可以通过将其更改为以下内容来轻松监听按键事件:

myQuitBtn.addEventListener(MouseEvent.CLICK, quit, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler, false, 0, true);

function keyUpHandler(e:KeyboardEvent):void {
    //Check if the key pressed was the enter key
    if(e.keyCode === 13){   //could also do `e.keyCode === Keyboard.ENTER`
       quit(); 
    }
}

//make the event argument optional so you can call this method directly and with a mouse listener
function quit(e:Event = null):void {
    //remove the key listener
    stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler, false);

    //do something, we quit the game
}