Well, this week, I was doing a little game that to the player start to play needs click in one “start game” button and after uses the keyboard to control the character moves. So until this point all OK, but what i didn’t told you is that when click’s in the “start game”, it disappears with one removeChild() (because it appears above all the stage).
After this, my character was no more moving… I fought “wow could be my InputManager class not working so fine…”, but I tried find some bug or something wrong and nothing. After some tries, accidentally I removed the buttonMode = true – propertie that i was defining to that my “start game” Movie Clip behaviors like Button – and tcharam… All working…..
At first we need remind that Keyboard events will just works when Flash Player get focus. And accidentally I found a way of Flash Player loses focus with on removeChild().
So what is happening here?!
First, open the swf file (focus it); after, click at the button (button with focus); later on, the button is removed from the stage (the focus is removed together with the button – so here are the problem). Now if I click on the stage, or other local in the swf file (Flash Player will get focus again) and it will works…
Here is a source code as example :
var spBtn = new Sprite();
spBtn.buttonMode = true; spBtn.graphics.beginFill( 0xff0000, .4 );
spBtn.graphics.drawRect( 0, 0, this.stage.stageWidth, this.stage.stageHeight );
spBtn.graphics.endFill();
addChild( spBtn );
addEventListener ( KeyboardEvent.KEY_DOWN, managekeyDown );
spBtn.addEventListener( MouseEvent.MOUSE_DOWN, mouseClick );
function mouseClick ( e : Event ) : void
{
spBtn.removeEventListener( MouseEvent.MOUSE_DOWN, mouseClick );
removeChild( spBtn );
}
function managekeyDown ( e : KeyboardEvent ) : void
{
if ( e.keyCode == Keyboard.LEFT )
{
trace ( "JUST TRACE IF FLASH PLAYER HAVE FOCUS ON IT" );
}
}
======= Updated at August 1º (expect update more the blog…) ======
To fix this issue you must reset the focus to the stage. So the code will change just in this part:
function mouseClick ( e : Event ) : void
{
spBtn.removeEventListener( MouseEvent.MOUSE_DOWN, mouseClick );
removeChild( spBtn );
//After the remove child we’ll reset the focus to stage
this.stage.focus = this;
}