PDA

View Full Version : Keyboard Loss of Focus


LivestockTony
07-29-2008, 11:54 PM
FlashDevelop/mxmlc AS3 only environment-

I've been working on my first flash game, which uses asdw and ijkl keys for movement... I had the gameplay working, and moved on to adding menus, etc... I still register the keyhandler to the stage on startup to allow checking the m keyup event to turn the music on/off

I have noticed, that when the user clicks on my SimpleButton for New Game, the keyboard loses focus until the user clicks on the flash window again. stage.hasEventListener(KeyboardEvent.Key_UP) traces as true in my render loop, but my keyhandler doesn't won't trace no matter what the user presses once in the game unless the user clicks on the screen.

(My keyhandler is basically a large switch-case statement, I put the trace before the switch in the function)

The function called by the new game button calls two functions, one unregisters the mouseEvents associated with the buttons on screen, then calls removeChild on all the menu screen objects. The second function calls my game_setup() function.

I've been into the Adobe docs, and I can't find it but am I right in thinking I'm losing focus because I've removed all of the objects on screen before adding the game objects? If so, what's the preferred way to avoid that? Is there a stage.whatever setting which forces keyboard focus?

Thanks,
-TF

devil_ether
07-30-2008, 02:11 AM
Hey Livestock,

Having the exact same problem as you (posted in the AS 3.0 forum -link below). It's driving me slowly mad...if you do find out a solution would you please post it/let me know?? If I find one I'll send it on to ya...

http://www.actionscript.org/forums/showthread.php3?t=178413

Cheers

devil_ether
07-30-2008, 02:32 AM
Ok...have just done a bit more digging and have found something that works for me.

when you return from your button (or whatever function is causing the loss of focus) add in:
stage.focus = yourMovieClip;

Not sure if this is the ideal/correct way of managing this but at least it works..

LivestockTony
07-30-2008, 03:21 AM
Thank you!! That indeed worked.
-TF