Tweener: following a path
package {
import caurina.transitions.Tweener;
import caurina.transitions.properties.CurveModifiers;
import flash.display.Sprite;
import flash.text.TextField;
public class TweenerPath extends Sprite {
// following a path with Tweener
// http://makc.coverthesky.com/FlashFX/ffx.php?id=15
private var fwd:Array = [];
private var bwd:Array = [];
private var mc:Sprite;
public function TweenerPath () {
// init Tweener
CurveModifiers.init();
// make 20 random checkpoints
var i:int;
for (i = 0; i < 20; i++) {
mc = new Sprite;
mc.graphics.beginFill (0xFF00);
mc.graphics.drawCircle (0, 0, 3);
mc.graphics.endFill ();
mc.x = 10 + 440 * Math.random ();
mc.y = 10 + 440 * Math.random ();
addChild (mc);
var t:TextField = new TextField;
t.text = (i + 1).toString (); mc.addChild (t);
var obj:Object = { x: mc.x, y: mc.y };
fwd.push (obj);
bwd.push (obj);
}
fwd.unshift (fwd [0]); fwd.push (fwd [fwd.length -1]);
bwd.unshift (bwd [0]); bwd.push (bwd [bwd.length -1]);
bwd.reverse ();
// make moving thing
mc = new Sprite;
mc.graphics.beginFill (0xFF7F00);
mc.graphics.drawCircle (0, 0, 3);
mc.graphics.endFill ();
addChild (mc);
goForward ();
}
private function goForward ():void
{
mc.x = fwd [0].x;
mc.y = fwd [0].y;
Tweener.addTween(mc, {
x:fwd [fwd.length -1].x,
y:fwd [fwd.length -1].y,
_bezier:makeBesierArray (fwd),
time:30, onComplete:goBackward, transition:"linear"});
}
private function goBackward ():void
{
mc.x = bwd [0].x;
mc.y = bwd [0].y;
Tweener.addTween(mc, {
x:bwd [bwd.length -1].x,
y:bwd [bwd.length -1].y,
_bezier:makeBesierArray (bwd),
time:30, onComplete:goForward, transition:"linear"});
}
private function makeBesierArray (p:Array):Array
{
var bezier:Array = [];
// convert all points between p[0] and p[last]
for (var i:int = 1; i < p.length -2; i++)
{
var b1:Object = {}, b2:Object = {};
// use p[0] properties to fill bezier array
for (var prop:String in p[0])
{
b1[prop] = -p[i -1][prop]/6 +p[i][prop] +p[i +1][prop]/6;
b2[prop] = +p[i][prop]/6 +p[i +1][prop] -p[i +2][prop]/6;
}
bezier.push (b1); bezier.push (b2);
}
return bezier;
}
}
}