PDA

View Full Version : [AS3] Stage-level KeyManager instance "losing focus"



kmintmier
October 10th, 2007, 09:44 PM
Greetings, all. This is my first post. I've learned a lot from this forum and now I come seeking help. I'm working on some game interface components in AS3 and I've got an issue that I haven't been able to solve.

I've implemented a KeyManager class that's very similar to the one found on this forum. I'm instantiating it as a variable called "keys" in the constructor of the document class. It works fine for almost everything. Now, enter my menu component.

I've created a set of classes that build interactive menus. As you might imagine, there's a Menu class that instantiates a MenuTitle object and multiple MenuOption objects. Since Menu objects should be keyboard-controllable, the ENTER_FRAME event of an instantiated Menu object needs access to the stage-level "keys" variable.

Hence, I'm passing "keys" as a parameter to the Menu constructor and storing a reference to "keys" in the Menu object. Then, I'm using said reference in the Menu's ENTER_FRAME event. Notably, the REMOVED_FROM_STAGE event of the Menu object nullifies the local reference to "keys".

The first time a Menu object is instantiated, it works as designed. The second time (and subsequent times) that a Menu object is instantiated, the Menu seems to "lose focus", meaning that the user has to click the stage before the keys work again.

I'm considering moving the keyboard-triggered actions out to the Document class and creating helper functions in the Menu class, alleviating the need to pass "keys" to the constructor. I'd like to keep it as-is since the local reference is, logically, the fastest route to the current key information.

Does anyone have any thoughts on what might cause the "loss of focus" that I'm seeing?

Thanks in advance.

SlowRoasted
October 10th, 2007, 10:15 PM
Just curious, is it happening in all browsers?

kmintmier
October 10th, 2007, 10:35 PM
Just curious, is it happening in all browsers?

I've only tested it in IE7 and FF 2.0.0.7 on Windows XP, but both browsers exhibit the same behavior.

BlueDragon
February 21st, 2008, 11:34 AM
use stage.focus = yourMovieClip; to set the focus to yourMovieClip
and stage.stageFocusRect = false; to hide the focus rect