弹性多个物体
1.构建小球精灵
Ball.as
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 ();
}
}
}
2.构建两个精灵的弹性动画
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 ();
}
}
}
DoubleSpring.as
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class DoubleSpring extends Sprite
{
private var ball0:Ball;
private var ball1:Ball;
private var ball0Dragging:Boolean=false;
private var ball1Dragging:Boolean=false;
private var spring:Number=0.1;
private var friction:Number=0.8;
private var springLength:Number=150;
public function DoubleSpring()
{
Init();
}
private function Init():void
{
ball0=new Ball(20,0xFF0000);
ball0.x=Math.random()*stage.stageWidth;
ball0.y=Math.random()*stage.stageHeight;
ball0.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
addChild(ball0);
ball1=new Ball(20,0xFF0000);
ball1.x=Math.random()*stage.stageWidth;
ball1.y=Math.random()*stage.stageHeight;
ball1.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
addChild(ball1);
addEventListener(Event.ENTER_FRAME,EnterFrame);
stage.addEventListener(MouseEvent.MOUSE_UP,MouseUp);
}
private function EnterFrame(e:Event):void
{
if(!ball0Dragging)
{
SpringTo(ball0,ball1);
}
if(!ball1Dragging)
{
SpringTo(ball1,ball0);
}
graphics.clear();
graphics.lineStyle(1);
graphics.moveTo(ball0.x,ball0.y);
graphics.lineTo(ball1.x,ball1.y);
}
private function SpringTo(ballA:Ball,ballB:Ball):void
{
var dx:Number=ballB.x-ballA.x;
var dy:Number=ballB.y-ballB.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)
{
ball0Dragging=true;
}
if(e.target==ball1)
{
ball1Dragging=true;
}
}
private function MouseUp(e:MouseEvent):void
{
ball0.stopDrag();
ball1.stopDrag();
ball0Dragging=false;
ball1Dragging=false;
}
}
}
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class DoubleSpring extends Sprite
{
private var ball0:Ball;
private var ball1:Ball;
private var ball0Dragging:Boolean=false;
private var ball1Dragging:Boolean=false;
private var spring:Number=0.1;
private var friction:Number=0.8;
private var springLength:Number=150;
public function DoubleSpring()
{
Init();
}
private function Init():void
{
ball0=new Ball(20,0xFF0000);
ball0.x=Math.random()*stage.stageWidth;
ball0.y=Math.random()*stage.stageHeight;
ball0.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
addChild(ball0);
ball1=new Ball(20,0xFF0000);
ball1.x=Math.random()*stage.stageWidth;
ball1.y=Math.random()*stage.stageHeight;
ball1.addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
addChild(ball1);
addEventListener(Event.ENTER_FRAME,EnterFrame);
stage.addEventListener(MouseEvent.MOUSE_UP,MouseUp);
}
private function EnterFrame(e:Event):void
{
if(!ball0Dragging)
{
SpringTo(ball0,ball1);
}
if(!ball1Dragging)
{
SpringTo(ball1,ball0);
}
graphics.clear();
graphics.lineStyle(1);
graphics.moveTo(ball0.x,ball0.y);
graphics.lineTo(ball1.x,ball1.y);
}
private function SpringTo(ballA:Ball,ballB:Ball):void
{
var dx:Number=ballB.x-ballA.x;
var dy:Number=ballB.y-ballB.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)
{
ball0Dragging=true;
}
if(e.target==ball1)
{
ball1Dragging=true;
}
}
private function MouseUp(e:MouseEvent):void
{
ball0.stopDrag();
ball1.stopDrag();
ball0Dragging=false;
ball1Dragging=false;
}
}
}