让小球沿着我画的线走

package
{
	import flash.display.Sprite;
	public class Ball extends Sprite
	{
		private var radius:Number = 5;
		private var color:Number = 0x3200ff;
		
		public function Ball()
		{		
			init();
		}
		public function init():void
		{
			this.graphics.beginFill(color);
			this.graphics.drawCircle(0, 0, radius);
			this.graphics.endFill();		
		}
	}
}
package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class FallowRoad extends Sprite
	{
		private var ball:Ball = new Ball  ;
		private var array:Array = new Array  ;
		private var num:Number = new Number  ;

		public function FallowRoad()
		{
			addChild(ball);
			//ball.y = 100;
			this.graphics.lineStyle(1,0x00ff00);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,_onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP,_onMouseUp);
		}

		public function _onMouseDown(event:MouseEvent):void
		{
			array.push(mouseX,mouseY);
			this.graphics.moveTo(mouseX,mouseY);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,_onMouseMove);
		}

		public function _onMouseMove(event:MouseEvent):void
		{
			array.push(event.stageX,event.stageY);
			this.graphics.lineTo(event.stageX,event.stageY);
		}

		public function _onMouseUp(event:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,_onMouseMove);
			addEventListener(Event.ENTER_FRAME,_onEnterFrame);
		}

		public function _onEnterFrame(event:Event):void
		{
			ball.x = array[num++];
			ball.y = array[num++];

			if (num >= array.length)
			{
				removeEventListener(Event.ENTER_FRAME,_onEnterFrame);
			}
		}
	}
}
/*
var arr:Array = [630,240,566,240,566,106];
var num:int = 0;
for (var i:int=0; i<3; i++)
{
	var b:int = arr[num++];
	var c:int = arr[num++];
	trace("b======="+b);
	trace("c======="+c);
}
*/

posted on 2011-03-22 11:10  602147629  阅读(301)  评论(0编辑  收藏  举报