Gamua / Starling-Framework

The Cross Platform Game Engine
http://www.starling-framework.org
Other
2.85k stars 819 forks source link

KeyboardEvent didnt work. #896

Closed AvetisSargsian closed 8 years ago

AvetisSargsian commented 8 years ago

Hello Daniel,I'd like to thank you for great job you did! Recently I migrated from starling 1.8 to 2.0.1 and notice that my code which duty to handle keyboard events stopped working. I cheked, whenever I roll back my working brunch to starling 1.8 it work again. Here is my code:

    public class SceneMediator extends AbstractMediator
{
    public function SceneMediator()
    {
        super();
        nativeVIew.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
    }

    override public function dispose():void
    {
        nativeVIew.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
        super.dispose();
    }

    override protected function setNativeVIew():AbstractView
    {
        return new Scene();
    }

    protected function onKeyboardBACK():void {}

    private function onKeyDown(event:KeyboardEvent):void
    {
        switch(event.keyCode)
        {
            case Keyboard.ESCAPE:
            case Keyboard.BACK:
                onKeyboardBACK();
                event.preventDefault();
                break;
            default:
                break;
        }
    }
}

It is never enter in " function onKeyDown(event:KeyboardEvent):void "

PrimaryFeather commented 8 years ago

Thanks a lot for the compliments! :smile:

The reason that this code doesn't work any longer is that I had to change how KeyboardEvents are dispatched. Before, they were broadcast to all display objects on the stage. However, that lead to performance issues if people had lots of objects on the screen.

For this reason, I'm now dispatching them only to the stage. Thus, it's a little more effort to listen to them.

The easiest way to listen to those events now is like this:

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

Actually, I recommend doing it like the following, though:

// in the class constructor:
stage.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
stage.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);

// instance methods
private function onAddedToStage():void
{
    stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}

private function onRemovedFromStage():void
{
    stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}

That way, it can't happen that the object still listens to those events, even if it has been removed from the stage.

Yeah, I preferred the old way, too. But those performance issues forced me into this change. ;-)

PrimaryFeather commented 8 years ago

I also just added this information to the migration guide. Thanks a lot for making me aware this was lacking!

AvetisSargsian commented 8 years ago

thanks a lot for quick answer, that helps! I also think that performance is first of all!

PrimaryFeather commented 8 years ago

You're welcome! Thanks for your understanding! I'll close this issue, then.