views:

1727

answers:

3

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!

+1  A: 

Add your keyboard event listeners to the stage instead of to your class.

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
Kekoa
that doesn't work, it tells me it can't reference a null object when I do that.
Matt
That's because you can't reference the stage until you are on it. fenomas has the solution to that.
Tyler Egeto
Yes, if your stage is not yet constructed, you must do that extra work.
Kekoa
It's not that the stage must be constructed, the display object must be part of the stage's display list. It doesn't have to be visible, but it must be a child or grandchild (etc.) of the stage.
fenomas
+10  A: 

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);
    }
fenomas
A: 

thank you...