"Action is the real measure of intelligence." - Napoleon Hill
AS3 KeyboardEvent
By Tommy Leung on 08/02/2008 in Games
I’ve been working with ActionScript 3 a little bit more as of late and one rather confusing problem that seems to come up often is one with KeyboardEvent–it doesn’t seem to always work. The solution that I’ve come up with is to always add the KeyboardEvent listener to the stage of the root.
So, you will want to use addEventListener in the main class:
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
And that’s it! This way has worked well for me so far. I actually use a KeyManager class to initialize the events and the stage is passed to that class.
| By Tommy Leung |

Comment



Thank you so much!! I’ve been pulling my hair out over here trying to figure out why my KEY_DOWN event listener worked in one game, but not in another, when I pasted the code exactly. This fixed it.