1

I have two classes. The first one (the starting class):

package
{
 import flash.display.Sprite;
 import flash.events.KeyboardEvent;
 import tetris.*;

 public class TetrisGame extends Sprite
 {

  private var _gameWell:Well;

  public function TetrisGame()
  {    
   _gameWell = new Well();
   addChild(_gameWell);
  } 
 }
}

The second:

package tetris
{
 import flash.display.Sprite;
 import flash.events.KeyboardEvent;

 public class Well extends Sprite
 {

  public function Well()
  {
   super();

   addEventListener(KeyboardEvent.KEY_DOWN, onKeyboard);
  }

  private function onKeyboard(event:KeyboardEvent):void
  {
   //some code is here
  }


 }
}

But when I press any buttons on my keyboard, the child class Well doesn't have any reaction. What's the problem?

3
  • If you set a breakpoint in onKeyboard, does it get triggered?
    – Robusto
    Mar 21, 2010 at 20:24
  • If you put a trace statement inside onKeyboard does that ever get written to the output panel?
    – jessegavin
    Mar 21, 2010 at 20:38
  • One thing to note about event listeners: they're strongly-referenced. If you GC the object holding the event listener (in this case, the "Well" object, the listener will persist, causing a memory leak. There are times to use strong-types (I've noticed a need for this when opening/closing dialogs w/event listeners), but if you can, try using this: addEventListener(<Event type>, <function>, false, 0, true); This preserves the default values for "useCapture" and "priority", but sets the "useWeakReference" property to "true". When "Well" is GC'd, the event listener will be also.
    – bedwyr
    Mar 22, 2010 at 3:09

2 Answers 2

2

OK, I get it! =))

I should set focus on the child sprite so it can listen for keyboard events.

package
{
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    import tetris.*;

    public class TetrisGame extends Sprite
    {

        private var _gameWell:Well;

        public function TetrisGame()
        {    
            _gameWell = new Well();
            addChild(_gameWell);

            stage.focus = _gameWell;
        } 
    }

}

1

Or as an alternative; add the event listener to the stage, so it doesn't depend on the Well having focus.

package tetris
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;

    public class Well extends Sprite 
    {

        public function Well():void 
        {
            super();

            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboard);
        }

        private function onKeyboard(event:KeyboardEvent):void
        {
            //some code is here
        }
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.