抛物线、碰撞、反弹

package
{
	import flash.display.Sprite;
	
	public class Ball extends Sprite
	{
		public var radius:Number;
		public var color:uint;
		public var vx:Number=0;
		public var vy:Number=0;
		
		public function Ball (radius:Number,color:uint)
		{
			this.radius=radius;
			this.color=color;
			Init ();
		}
		public function Init ():void
		{
			graphics.beginFill (color);
			graphics.drawCircle (0,0,radius);
			graphics.endFill ();
		}
	}
}
package {
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.MouseCursor;
	import flash.ui.Mouse;
	
	public class Throwing extends Sprite {
		
		private var ball:Ball;
		private var vx:Number;
		private var vy:Number;
		private var bounce:Number=-0.8;
		private var gravity:Number=0.75;
		private var frictionX:Number = 0.98;
		private var frictionY:Number = 0.99;
		private var oldX:Number;
		private var oldY:Number;
		
		public function Throwing() {
			init();
		}
		
		private function init():void {
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;
			ball = new Ball(30,0x000000);
			ball.x = stage.stageWidth/2;
			ball.y = stage.stageHeight/2;
			vx = Math.random()*10-5;
			vy = -10;
			addChild(ball);
			ball.addEventListener(MouseEvent.MOUSE_DOWN, MouseDownHandler);
			ball.addEventListener(MouseEvent.MOUSE_OVER,function(){Mouse.cursor = MouseCursor.HAND;});
			ball.addEventListener(MouseEvent.MOUSE_OUT,function(){Mouse.cursor = MouseCursor.AUTO;});
			addEventListener(Event.ENTER_FRAME, EnterFrameHandler);
		}
		
		private function MouseDownHandler(event:MouseEvent):void {
			oldX = ball.x;
			oldY = ball.y;
			stage.addEventListener(MouseEvent.MOUSE_UP, MouseUpHandler);
			ball.startDrag();
			removeEventListener(Event.ENTER_FRAME, EnterFrameHandler);
			addEventListener(Event.ENTER_FRAME, TrackVelocity);
		}
		
		private function EnterFrameHandler(event:Event):void {
			vy += gravity;
			vx *= frictionX;
			vy *= frictionY;
			ball.x += vx;
			ball.y += vy;
			var left:Number=0;
			var right:Number=stage.stageWidth;
			var top:Number=0;
			var bottom:Number=stage.stageHeight;
			if (ball.x+ball.radius>right) {
				ball.x=right-ball.radius;
				vx*=bounce;
			} else if (ball.x - ball.radius < left) {
				
				ball.x=left+ball.radius;
				vx*=bounce;
			}
			if (ball.y+ball.radius>bottom) {
				ball.y=bottom-ball.radius;
				vy*=bounce;
			} else if (ball.y - ball.radius < top) {
				ball.y=top+ball.radius;
				vy*=bounce;
			}
		}
		
		//跟踪每一帧小球的速度(坐标位置)
		private function TrackVelocity(event:Event):void {
			vx = ball.x-oldX;
			vy = ball.y-oldY;
			oldX = ball.x;
			oldY = ball.y;
		}
		
		private function MouseUpHandler(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, MouseUpHandler);
			ball.stopDrag();
			removeEventListener(Event.ENTER_FRAME, TrackVelocity);
			addEventListener(Event.ENTER_FRAME, EnterFrameHandler);
		}
		
		
	}
}


你可能感兴趣的:(抛物线、碰撞、反弹)