forked from: flash on 2010-3-7
/**
* Copyright BrainCandy ( http://wonderfl.net/user/BrainCandy )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/5kag
*/
// forked from 084's flash on 2010-3-7
package {
import fl.transitions.easing.Back;
import flash.display.Sprite;
import flash.events.Event;
public class Chain extends Sprite {
private var balls:Array;
private var numBalls:Number = 5;
//private var ball0:Ball;
//private var ball1:Ball;
//private var ball2:Ball;
private var spring:Number = 0.01;
private var friction:Number = 0.5;
private var gravity:Number = 0;
public function Chain() {
init();
}
private function init():void {
balls = new Array();
for (var i:uint = 0; i < numBalls; i++) {
var ball:Ball = new Ball(20,Math.random()*0xff0000);
addChild(ball);
balls.push(ball);
}
//ball0 = new Ball(10);
//addChild(ball0);
//ball1 = new Ball(20);
//addChild(ball1);
//ball2 = new Ball(25);
//addChild(ball2);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
//moveBall(ball0, mouseX, mouseY);
//moveBall(ball1, ball0.x, ball0.y);
//moveBall(ball2, ball1.x, ball1.y);
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:uint = 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);
}
//graphics.lineTo(ball0.x, ball0.y);
//graphics.lineTo(ball1.x, ball1.y);
//graphics.lineTo(ball2.x, ball2.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;
}
}
}
import flash.display.Sprite;
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 = 40,color:uint=0xff0000 ):void {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}