Amy
Amy

Reputation: 946

AS3 keyboard events within a class?

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!

Upvotes: 2

Views: 11210

Answers (3)

wen
wen

Reputation: 21

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

Upvotes: 2

fenomas
fenomas

Reputation: 11139

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

Upvotes: 17

Kekoa
Kekoa

Reputation: 28230

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

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);

Upvotes: 2

Related Questions