topical media & game development
animation-ch08-ChainArray.ax
animation-ch08-ChainArray.ax
[swf]
[flash]
flex
package
{
import flash.display.Sprite;
import flash.events.Event;
public class @ax-animation-ch08-ChainArray extends Sprite
{
private var balls:Array;
private var numBalls:Number = 5;
private var spring:Number = 0.1;
private var friction:Number = 0.8;
private var gravity:Number = 5;
public function @ax-animation-ch08-ChainArray()
{
init();
}
init(s)
private function init():void
{
balls = new Array();
for(var i:uint = 0; i < numBalls; i++)
{
var ball:animation_ch08_Ball = new animation_ch08_Ball(20);
addChild(ball);
balls.push(ball);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
frame(s)
private function onEnterFrame(event: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:uint = 1; i < numBalls; i++)
{
var ballA:animation_ch08_Ball = balls[i-1];
var ballB:animation_ch08_Ball = balls[i];
moveBall(ballB, ballA.x, ballA.y);
graphics.lineTo(ballB.x, ballB.y);
}
}
move(s)
private function moveBall(ball:animation_ch08_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;
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.