[ActionScript 3.0] AS3 弹性运动

 1 package com.views
 2 {
 3     import flash.display.Sprite;
 4     import flash.events.Event;
 5     import flash.geom.Point;
 6     
 7     /**
 8      * @author Frost.Yen
 9      * @E-mail 871979853@qq.com
10      * @create 2015-8-20 上午11:16:11
11      *
12      */
13     [SWF(width="800",height="600")]
14     public class ElasticMove extends Sprite
15     {
16         private const SPRING:Number=0.1;//弹性系数 
17         private const FRICTION:Number=0.9;//摩擦系数 
18         private const FEAR_DISTANCE:Number=150;//安全距离(小于该距离则发生躲避行为) 
19         private const MAX_AVOID_FORCE:uint=10;//最大躲避速度 
20         private var _destinationPoint:Point;//目标静止点(鼠标远离该物体时,物体最终会静止的坐标点) 
21         private var _speed:Point=new Point(0,0);//速度矢量(_speed.x即相当于vx,_speed.y即相当于vy) 
22         public function ElasticMove()
23         {
24             super();
25             drawStuff(); 
26             destinationPoint = new Point(300,300);
27         }
28         private function drawStuff():void { 
29             //默认先画一个半径为10的圆 
30             graphics.beginFill(Math.random() * 0xFFFFFF); 
31             //graphics.beginFill(0xFFFFFF); 
32             graphics.drawCircle(0, 0, 5); 
33             graphics.endFill(); 
34         }
35         //只写属性(设置目标点) 
36         public function set destinationPoint(value:Point):void { 
37             _destinationPoint=value; 
38             addEventListener(Event.ENTER_FRAME, enterFrameHandler); 
39         }
40         protected function enterFrameHandler(e:Event):void { 
41             moveToDestination();//先移动到目标点 
42             avoidMouse();//躲避鼠标 
43             applyFriction();//应用摩擦力 
44             updatePosition();//更新位置 
45         }
46         //以弹性运动方式移动到目标点 
47         private function moveToDestination():void { 
48             _speed.x += (_destinationPoint.x - x) * SPRING; 
49             _speed.y += (_destinationPoint.y - y) * SPRING; 
50         }
51         //躲避鼠标 
52         private function avoidMouse():void { 
53             var currentPosition:Point=new Point(x,y);//确定当前位置 
54             var mousePosition:Point=new Point(stage.mouseX,stage.mouseY);//确实鼠标位置 
55             var distance:uint=Point.distance(currentPosition,mousePosition);//计算鼠标与当前位置的距离 
56             //如果低于安全距离 
57             if (distance<FEAR_DISTANCE) { 
58                 var force:Number = (1 - distance / FEAR_DISTANCE) * MAX_AVOID_FORCE;//计算(每单位时间的)躲避距离--即躲避速率 
59                 var gamma:Number=Math.atan2(currentPosition.y- mousePosition.y,currentPosition.x- mousePosition.x);//计算鼠标所在位置与当前位置所成的夹角 
60                 var avoidVector:Point=Point.polar(force,gamma);//将极坐标转换为普通(笛卡尔)坐标--其实相当于vx = force*Math.cos(gamma),vy = force*Math.sin(gamma) 
61                 //加速 躲避逃开 
62                 _speed.x+=avoidVector.x; 
63                 _speed.y+=avoidVector.y; 
64             }
65         }
66         //应用摩擦力 
67         private function applyFriction():void { 
68             _speed.x*=FRICTION; 
69             _speed.y*=FRICTION; 
70         }
71         //最终更新自身的位置 
72         private function updatePosition():void { 
73             x+=_speed.x; 
74             y+=_speed.y; 
75         }
76     }
77 }

 

posted on 2015-08-20 17:50  晏过留痕  阅读(470)  评论(0编辑  收藏  举报