`
china34420
  • 浏览: 137910 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

弹性运动(三):弹簧链

阅读更多
package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.Graphics;
	
	/**
	 * ...弹簧链
	 * @author zkl
	 * 来自:Action Script3.0 Animation
	 */
	public class ChainArray extends Sprite 
	{
		private var balls:Array;
		private var numballs:int = 5;
		private var spring:Number = 0.1;
		private var friction:Number = 0.95;
		private var gravity:Number = 5;
		
		public function ChainArray():void
		{
		    init();	
		}
		private function init():void
		{
		    balls = new Array;
			for (var i:int = 0; i < numballs; i++ ) {
				var ball:Ball = new Ball(20);
				ball.vx = 0;
				ball.vy = 20;
				addChild(ball);
				balls.push(ball);
			}
			addEventListener(Event.ENTER_FRAME, EnterFrame);
		}
		private function EnterFrame(e:Event):void
		{
			graphics.clear();
			graphics.lineStyle(1);
			graphics.moveTo(mouseX, mouseY);
			moveBall(balls[0], mouseX, mouseY);
			graphics.lineTo(balls[0].x, balls[0].y);
			
			for (var i:int = 1; i < numballs; i++ ) {
				var ballA:Ball = balls[i - 1];
				var ballB:Ball = balls[i];
				moveBall(ballB, ballA.x, ballA.y);
				graphics.lineTo(ballB.x, ballB.y);
			}
		}
		private function moveBall(ball:Ball, targetX:Number, targetY:Number):void
		{
			ball.vx = (targetX - ball.x) * spring;
			ball.vy = (targetY - ball.y) * spring;
			ball.vy += gravity;
			ball.vx *= friction;
			ball.vy *= friction;
			ball.x += ball.vx;
			ball.y += ball.vy;
		}
	}
	
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics