[英]Linking Enter key to button in Flash game
我正在使用ActionScript 3.0在Adobe Flash中構建一個地形培訓師。 我現在快完成了。 我在游戲結束時做了這個“退出”按鈕。 您可以單擊它立即退出游戲,但是我也想按Enter鍵對退出按鈕做出反應。 我確實嘗試過在Internet和stackoverflow上查找它,但是我的搜索技能不夠先進,或者還沒有一個人遇到相同的問題。
我希望有人知道如何將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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.