Been working in IT since 2000, mostly in VB* till 3 years ago, when i migrated to Flash and AS3. Made several projects that combine AS3 with PHP. Mostly worked on games and Desktop applications.View all articles by Tecsi Aron
You are making a pac-man game, you want your user to have some control over the game so you place a menu button, you write the code for your menu. After you did this you notice that you no longer can control your pacman. After some headache you find out that if you click anything (except the menu button), your games works again. More technically speaking after doing removeChild to remove your menu your KeyboardEvent.KEY_DOWN/UP added to stage isn't firring any more. Example:
Just as described above you will see that the label at the top of the swf indicates correctly if a button is pressed or not UNTIL I remove the menu. Code is as follows:
textField.text="Key not pressed";
function destroyM(e:Event)//function called from mainM that tells the SWF that the menu needs to close.
"menu" is in my library and has Export for ActionScript.
So here's the part most people don't figure out (because its not really rational): though i did remove the menu from the stage, and eliminated any reference to it, since i clicked on the "Close Me" button it got focus, and it kept the focus even after being removed. (This is the irrational part... why would you want the focus on something that you cant interact with.). So let's recap, stage.addEventListener(KeyboardEvent.KEY_DOWN,example) is supposed to bubble to the stage, but the item that has focus, and there for is getting the event is no longer a child of the stage, this means the event will never bubble up back to the stage.
The solution for this long and stupid problem is one tiny line:
In my case all i have to do is to add this line:
to my destroyM function. The result being:
Hope someone will find this helpful.