ActionScript 3 KeyboardEvent not firing

这一生的挚爱 提交于 2019-12-08 23:11:34

You need to add the listeners to the stage:

private function init(e:Event = null):void
{
    removeEventListener(Event.ADDED_TO_STAGE,init);

    stage.addEventListener(MouseEvent.CLICK, onClickEvent);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownEvent);
}

You may need to be sure the movieclip listening for the event has focus. If you have some sort of menu or start screen for your game that you click a button on to start, that movieclip can retain focus even if it has been removed from stage.

Here's a good article explaining this in further detail: Keyboard events on stage are not working...?

If you are in the Flash IDE (for CS4 at least) you will also need to disable the keyboard shortcuts when you debug (Ctrl+Shift+Enter) or test (Ctrl+Enter) the fla.

In the movie window, select 'Control'->'Disable Keyboard Shortcuts'. This will stop the Flash IDE recieving the keyboard input, and allow the debug/test window to instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!