【问题标题】:Linking Enter key to button in Flash game在 Flash 游戏中将 Enter 键链接到按钮
【发布时间】:2016-03-01 17:04:17
【问题描述】:

我正在使用 actionscript 3.0 在 Adob​​e Flash 中构建一个 topo-trainer。 我现在差不多完成了。我在游戏结束时制作了这个“退出”按钮。 您现在可以单击它退出游戏,但我希望 Enter 键也能对退出按钮做出反应。我确实尝试在互联网和 stackoverflow 上查找它,但要么我的搜索技能不够先进,要么还没有人遇到同样的问题。

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

感谢您的帮助,

贾斯汀

【问题讨论】:

    标签: actionscript-3 flash button adobe enter


    【解决方案1】:

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

    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
    }
    

    【讨论】:

    • 嘿BadFeelingAboutThis,
    • 你是只是打个招呼,还是有问题过早地按回车?
    • 对不起,我失败了。当我想获得另一个空间时按回车键。但是非常感谢!你帮我解决了!不知道这么简单。感谢您的宝贵时间!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 2014-01-03
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2018-05-08
    相关资源
    最近更新 更多