简体   繁体   English

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

[英]Linking Enter key to button in Flash game

I'm building a topo-trainer in Adobe Flash with actionscript 3.0. 我正在使用ActionScript 3.0在Adobe Flash中构建一个地形培训师。 I'm almost done now. 我现在快完成了。 I made this "quit" button at the end of the game. 我在游戏结束时做了这个“退出”按钮。 You can click it to quit the game now, but I would like the Enter key to react to the quit-button as well. 您可以单击它立即退出游戏,但是我也想按Enter键对退出按钮做出反应。 I really tried looking it up on the internet and on stackoverflow, but either my searching skills are not advanced enough or there hasn't been a person with the same problem yet. 我确实尝试过在Internet和stackoverflow上查找它,但是我的搜索技能不够先进,或者还没有一个人遇到相同的问题。

I hope somebody knows how to couple the Enter button to a button in Flash. 我希望有人知道如何将Enter按钮与Flash中的按钮耦合。 There is no EditText involved. 没有涉及EditText。

Thanks for your help, 谢谢你的帮助,

Justin 贾斯汀

Let's say you have some code that runs when your games ends: 假设您有一些在游戏结束时运行的代码:

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

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

You could easily listen for a key event by changing it to the following: 您可以通过将其更改为以下内容来轻松监听关键事件:

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
}

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

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