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

弹簧连接多个物体(目标偏移)

阅读更多
弹簧连接多个物体(目标偏移)
package 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.display.Graphics;
	
	/**
	 * ...弹簧连接多个物体(目标偏移)
	 * @author zkl
	 * 来自:Action Script3.0 Animation
	 */
	public class DoubleSpring extends Sprite 
	{
		private var ball0:Ball;
		private var ball1:Ball;
		private var ball2:Ball;
		private var ball0Draging:Boolean = false;
		private var ball1Draging:Boolean = false;
		private var ball2Draging:Boolean = false;
		
		private var spring:Number = 0.1;
		//private var vx:Number = 0;
		//private var vy:Number = 0;
		private var friction:Number = 0.95;
		private var springLength:Number = 100;
		
		public function DoubleSpring():void
		{
			init();
		}
		
		private function init():void
		{
			ball0 = new Ball(20);
			addChild(ball0);
			ball0.x = Math.random() * stage.stageWidth;
			ball0.y = Math.random() * stage.stageHeight;
			ball0.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			
			ball1 = new Ball(20);
			addChild(ball1);
			ball1.x = Math.random() * stage.stageWidth;
			ball1.y = Math.random() * stage.stageHeight;
			ball1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			
			ball2 = new Ball(20);
			addChild(ball2);
			ball2.x = Math.random() * stage.stageWidth;
			ball2.y = Math.random() * stage.stageHeight;
			ball2.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			
			addEventListener(Event.ENTER_FRAME, EnterFrame);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
		}
		
		private function EnterFrame(e:Event):void
		{
			if (!ball0Draging) {
				springTo(ball0, ball1);
				springTo(ball0, ball2);
			}
			if (!ball1Draging) {
				springTo(ball1, ball0);
				springTo(ball1, ball2);
			}
			if (!ball2Draging) {
				springTo(ball2, ball0);
				springTo(ball2, ball1);
			}
			graphics.clear();
			graphics.lineStyle(1);
			graphics.moveTo(ball0.x, ball0.y);
			graphics.lineTo(ball1.x, ball1.y);
			graphics.lineTo(ball2.x, ball2.y);
		}
		
		private function springTo(ballA:Ball,ballB:Ball):void
		{
			var dx:Number = ballB.x - ballA.x;
			var dy:Number = ballB.y - ballA.y;
			var angle:Number = Math.atan2(dy, dx);
			var targetX:Number = ballB.x + Math.cos(angle) * springLength;
			var targetY:Number = ballB.y + Math.sin(angle) * springLength;
			ballA.vx += (targetX - ballA.x) * spring;
			ballA.vy += (targetY - ballA.y) * spring;
			ballA.vx *= friction;
			ballA.vy *= friction;
			ballA.x += ballA.vx;
			ballA.y += ballA.vy;
			
		}
		
		private function mouseDown(e:MouseEvent):void
		{
			e.target.startDrag();
			if (e.target == ball0) {
				ball0Draging = true;
			}
			if (e.target == ball1) {
				ball1Draging = true;
			}
			if (e.target == ball2) {
				ball2Draging = true;
			}
		}
		private function mouseUp(e:MouseEvent):void
		{
			ball0.stopDrag();
			ball1.stopDrag();
			ball2.stopDrag();
			ball0Draging = false;
			ball1Draging = false;
			ball2Draging = false;
		}
		
	}
	
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics