Closed AvetisSargsian closed 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. ;-)
I also just added this information to the migration guide. Thanks a lot for making me aware this was lacking!
thanks a lot for quick answer, that helps! I also think that performance is first of all!
You're welcome! Thanks for your understanding! I'll close this issue, then.
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:
It is never enter in " function onKeyDown(event:KeyboardEvent):void "