I have a Sprite
which is a child of a Flex object, and acts as the main "game window". So during game-play it should get keyboard input but at other times not. When I add a keyboard listener to it, it nev开发者_运维百科er fires.
Do I need to stop parent objects 'eating' the events somehow, or something similar? I get the same thing with mouse-wheel events, but mouse-click events are fine. Sprite
doesn't seem to have the concept of focus unless I missed it.
More generally in this kind of approach, are there any suggestions on the best way? For instance some keys might still be needed to be intercepted before getting to the game-window, like ESC or F1 or something... the aim is the game-window isn't aware of other UI.
Your sprite needs to have input focus to receive Keyboard Events.
As for best practices: If your entire application is based on keyboard input, and if you don't have to switch contexts ( the same key means a different thing if another object is selected ) a lot, you could just add the listeners to the stage.
Otherwise, you can use stage.focus to set the focus to the desired object.
In order to have your sprite focusable, you need to implement the interface mx.managers.IFocusManagerComponent
. Otherwise tab key will not work on the sprite. According to the adobe doc:
The IFocusManagerComponent interface defines the interface that focusable components must implement in order to receive focus from the FocusManager.
You can look at the code in UIComponent to find out how they implemented the functions defined in IFocusManagerComponent.
You should really listen for keyboard events on the root application or stage. Since actionscript has proper event bubbling you can always be sure the events will get there(if they're not consumed by a text box or other input). You can simulate them being directly on the sprite by adding eventlisteners to the stage when you need them for the sprite, and removing them from the stage when you dont.
or you could persist the key listeners on the stage and change a context
variable when the context of your application changes. Then you conditionalize the event listener's action on the context
of your application. One main advantage to this is that all your key event logic is in the same place and can be much more easily debugged.
As for the mouse wheel, if you're planning on having the game embedded on other sites you might as well not worry about it. They're probably not going to let you install some js hack to get it working on mac. Otherwise yeah, get the SWFObject add-on: MouseWheel on Mac OS .
精彩评论