topical media & game development
animation-ch08-Chain.ax
animation-ch08-Chain.ax
[swf]
[flash]
flex
package
{
import flash.display.Sprite;
import flash.events.Event;
public class @ax-animation-ch08-Chain extends Sprite
{
private var ball0:animation_ch08_Ball;
private var ball1:animation_ch08_Ball;
private var ball2:animation_ch08_Ball;
private var spring:Number = 0.1;
private var friction:Number = 0.8;
private var gravity:Number = 5;
public function @ax-animation-ch08-Chain()
{
init();
}
init(s)
private function init():void
{
ball0 = new animation_ch08_Ball(20);
addChild(ball0);
ball1 = new animation_ch08_Ball(20);
addChild(ball1);
ball2 = new animation_ch08_Ball(20);
addChild(ball2);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
frame(s)
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);
graphics.lineTo(ball0.x, ball0.y);
graphics.lineTo(ball1.x, ball1.y);
graphics.lineTo(ball2.x, ball2.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.