I seem to have trouble getting a keyboard event within a class to work, I have an ENTER_FRAME event which works fine, but the keyboard event never gets called. Any ideas? here's the code
package
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.*;
public class mainGame extends MovieClip
{
var myPlayer:player = new player();
function mainGame():void
{
trace("arg!");
addChild(myPlayer);
addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
addEventListener(Event.ENTER_FRAME, update);
}
function keyDown(evt:KeyboardEvent):void
{
trace("This never happens");
myPlayer.x++;
}
function update(evt:Event):void
{
trace("This happens fine");
}
}
}
thanks in advance!
Unlike in AS2, in AS3 Keyboard events are not global. They are issued to the stage, and they bubble through the display list to whatever display object has focus. In your code, your event listener is being added to mainGame, so it is only going to fire when mainGame is on the stage, and the user has clicked (or mouse-overed, I forget) the mainGame movie clip.
When you want to get keyboard events globally, as I said, they all start with the stage before bubbling, so if you register for events with the stage, you'll get all keyboard events. So as kekoav said, the way to do that is
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
However, your problem is that stage in that code is not a global variable. It's a property that is common to all DisplayObjects, but it is null until the object is added to the display list.
So to dispense with the explanations, the code above will work if you call it after mainGame has been added to the stage. If there's no easy way for you to do that, you could add your keyboard listener inside another listener that knows when to fire:
function mainGame():void {
//...
addEventListener(Event.ADDED_TO_STAGE, stageAddHandler);
//...
}
private function stageAddHandler(e:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
removeEventListener(Event.ADDED_TO_STAGE, stageAddHandler);
}
Add your keyboard event listeners to the stage instead of to your class.
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With