2 Replies Latest reply on Feb 8, 2008 8:55 PM by Isaac Fung

    Keyboard control of a ball

    Isaac Fung
      Hi, the following simple program was bogged down by the error message "1046: Type was not found or was not a compile-time constant: KeyboardEvent" at line 27 where myKeyboardEvent was defined. Please help.

      package {

      import flash.display.Sprite;
      import flash.events.KeyboradEvent;
      import flash.ui.Keyboard;

      public class ASProjects extends Sprite
      {
      private var ball:Sprite;

      public function ASProjects()
      {
      init();
      }

      private function init():void {
      ball = new Sprite();
      addChild(ball);
      ball.graphics.beginFill(0xff0000);
      ball.graphics.drawCircle(0,0,40);
      ball.graphics.endFill();
      ball.x = stage.stageWidth/2;
      ball.y = stage.stageHeight/2;
      stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyboardEvent);
      }

      public function myKeyboardEvent(event:KeyboardEvent):void {

      switch(event.keyCode) {
      case Keyboard.UP :
      ball.y -= 10;
      break;

      case Keyboard.DOWN :
      ball.y += 10;
      break;

      case Keyboard.LEFT :
      ball.x -= 10;
      break;

      case Keyboard.RIGHT :
      ball.x -= 10;
      break;

      default:
      break;
      }
      }
      }
      }