actionScript键盘控制 物体移动

package
{
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	public class plane extends Sprite
	{
		private var sprite:Sprite;
		private var speed:int;
		public function plane()
		{
			
			sprite=new Sprite();
			sprite.graphics.beginFill(0x2257D6,1);
			sprite.graphics.drawCircle(20,20,20);
			sprite.graphics.endFill();
			addChild(sprite);
			speeds();
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
		}
		function speeds():void
		{
			this.speed=5;
		}
		
		function onMouseMove(event:MouseEvent):void
		{
			this.sprite.x=mouseX;
			this.sprite.y=mouseY;
		}
		function onKeyDown(event:KeyboardEvent):void
		{
			switch(event.keyCode)
			{
				case Keyboard.LEFT:
				this.onLeft();
				trace("left");
				break;
				case Keyboard.RIGHT:
				this.onRight();
				trace("right");
				break;
				case Keyboard.UP:
				this.onUp();
				trace("up");
				break;
				case Keyboard.DOWN:
				this.onDown();
				trace("down");
				break;
			}
		}
		
		//向左
		function onLeft():void
		{
			if(this.sprite.x>15)
			{
				this.sprite.x-=this.speed;
			}
		}
		
		//向右
		function onRight():void
		{
			if(this.sprite.x<405)
			{
				this.sprite.x+=this.speed;
			}
		}
		
		//向上
		function onUp():void
		{
			if(this.sprite.y>20)
			{
				this.sprite.y-=this.speed;
			}
		}
		
		//向下
		function onDown():void
		{
			if(this.sprite.y<305)
			{
				this.sprite.y+=this.speed;
			}
		}

	}
}

 

 

 

 

你可能感兴趣的:(UI,Flash,UP,actionscript)