본문 바로가기

AS3/ActionScript

키보드 제어

package 
{
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	public class  KeyCodes extends Sprite
	{
		private var ball:Sprite;
		
		public function KeyCodes() {
			init();
		}
		
		private function init():void {
			ball = new Sprite();
			addChild(ball);
			ball.graphics.beginFill(0x000000);
			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 );
			
		}
		private 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;
			}
		}
	}
}


[Flash] http://nicekon.tistory.com/attachment/cfile4.uf@1950E00E49EBD83F7E731B.swf


키보드 방향키를 이용한 무비클립위치 이동시키기