使用组合键使小球在舞台内多方位移动

package {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;


	/*
	      *author : SinSoul 
	      *blogs: http://www.cnblogs.com/sinsoul
	 
	*/

	public class Main extends MovieClip {
		private var key37:Boolean;
		private var key38:Boolean;
		private var key39:Boolean;
		private var key40:Boolean;
		public function Main():void {

			init();

		}
		private function init():void {
			key37=false;
			key38=false;
			key39=false;
			key40=false;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeydown);
			stage.addEventListener(KeyboardEvent.KEY_UP,onkeyup);
			stage.addEventListener(Event.ENTER_FRAME,onevent);
		}


		private function onKeydown(e):void {
			//trace(1);
			switch (e.keyCode) {
				case 37 :
					key37=true;
					//trace("37");
					break;
				case 38 :
					key38=true;
					break;
				case 39 :
					//trace("39");
					key39=true;
					break;
				case 40 :

					key40=true;
					break;
			}


		}
		private function onkeyup(e):void {
			switch (e.keyCode) {
				case 37 :
					key37=false;
					//trace("37");
					break;
				case 38 :
					key38=false;
					break;
				case 39 :
					//trace("39");
					key39=false;
					break;
				case 40 :

					key40=false;
					break;
			}

		}
		private function onevent(e):void {
			if (key37==true) {
				mc.x-=10;
			}
			if (key38==true) {
				mc.y-=10;
			}
			if (key39==true) {
				mc.x+=10;
			}
			if (key40==true) {
				mc.y+=10;
			}

		}
	}
}

 

 

posted @ 2010-11-05 13:56  sinsoul  阅读(315)  评论(0编辑  收藏  举报