弹性链
1.构建小球精灵
Ball.as
package
{
import flash.display.Sprite;
public class Ball extends Sprite
{
public var radius:Number;
public var color:uint;
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 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 ();
}
}
}
Chain.as
package
{
import flash.events.Event;
import flash.display.Sprite;
public class Chain extends Sprite
{
private var balls:Array;
private var numBalls:Number=10;
private var friction:Number=0.8;
private var spring:Number=0.1;
private var gravity:Number=5;
private var vx:Number=stage.stageWidth/2;
private var vy:Number=stage.stageHeight/2;
public function Chain ()
{
Init ();
}
private function Init ():void
{
balls=new Array();
for (var i:int=0; i<numBalls; i++)
{
var ball:Ball=new Ball(20,0xFF0000);
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
{
vx+=(targetX-ball.x)*spring;
vy+=(targetY-ball.y)*spring;
vy+=gravity;
vx*=friction;
vy*=friction;
ball.x+=vx;
ball.y+=vy;
}
}
}
package
{
import flash.events.Event;
import flash.display.Sprite;
public class Chain extends Sprite
{
private var balls:Array;
private var numBalls:Number=10;
private var friction:Number=0.8;
private var spring:Number=0.1;
private var gravity:Number=5;
private var vx:Number=stage.stageWidth/2;
private var vy:Number=stage.stageHeight/2;
public function Chain ()
{
Init ();
}
private function Init ():void
{
balls=new Array();
for (var i:int=0; i<numBalls; i++)
{
var ball:Ball=new Ball(20,0xFF0000);
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
{
vx+=(targetX-ball.x)*spring;
vy+=(targetY-ball.y)*spring;
vy+=gravity;
vx*=friction;
vy*=friction;
ball.x+=vx;
ball.y+=vy;
}
}
}