AS3 keyboard events within a class?

11,155

Solution 1

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);
    }

Solution 2

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

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);

Solution 3

 import flash.events.Event;
 import flash.events.KeyboardEvent;

place your keyboard event inside the enterframe event, that way it updates everytime the frame is updated.

            function update(evt:Event):void     
            {     
                    trace("This happens fine"); 
                     function keyDown(evt:KeyboardEvent):void     
                     {     
                        trace("This never happens");     
                        myPlayer.x++;     
                     }     
            }  
Share:
11,155
Matt
Author by

Matt

I make stuff

Updated on June 15, 2022

Comments

  • Matt
    Matt almost 2 years

    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!

  • Matt
    Matt almost 15 years
    that doesn't work, it tells me it can't reference a null object when I do that.
  • Tyler Egeto
    Tyler Egeto almost 15 years
    That's because you can't reference the stage until you are on it. fenomas has the solution to that.
  • Kekoa
    Kekoa almost 15 years
    Yes, if your stage is not yet constructed, you must do that extra work.
  • fenomas
    fenomas almost 15 years
    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.