topical media & game development

talk show tell print

animation-ch02-KeyCodes.ax

animation-ch02-KeyCodes.ax [swf] [flash] flex


  package {
   import flash.display.Sprite;
   import flash.events.KeyboardEvent;
   import flash.ui.Keyboard;
   
   public class @ax-animation-ch02-KeyCodes extends Sprite {
    private var ball:Sprite;
    
    public function @ax-animation-ch02-KeyCodes() {
     init();
    }
  

init(s)


    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, onKeyboardEvent);
    }
  

keyboard(s)


    public function onKeyboardEvent(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;
     }
    }
   }
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.