Handling keyboard events for a particular object : KeyboardEvent : Development Flash / Flex / ActionScript examples


Flash / Flex / ActionScript examples » Development » KeyboardEvent »

 

Handling keyboard events for a particular object


 
package {
  import flash.display.*;
  import flash.events.*;

  public class Main extends Sprite {
    public function Main(  ) {
      var rect1:Sprite = new Sprite(  );
      rect1.graphics.lineStyle(1);
      rect1.graphics.beginFill(0x0000FF);
      rect1.graphics.drawRect(007575);
      rect1.tabEnabled = true;

      var rect2:Sprite = new Sprite(  );
      rect2.graphics.lineStyle(1);
      rect2.graphics.beginFill(0x0000FF);
      rect2.graphics.drawRect(007575);
      rect2.x = 200;
      rect2.tabEnabled = true;

      addChild(rect1);
      addChild(rect2);

      rect1.addEventListener(KeyboardEvent.KEY_DOWN, rect1KeyDownListener);
      rect2.addEventListener(KeyboardEvent.KEY_DOWN, rect2KeyDownListener);
    }

    private function rect1KeyDownListener (e:KeyboardEvent):void {
      Sprite(e.target).x += 10;
    }

    private function rect2KeyDownListener (e:KeyboardEvent):void {
      Sprite(e.target).rotation += 10;
    }
  }
}

        



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


Flash / Flex / ActionScript examples

 Navioo Development
» KeyboardEvent