In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: forked from: Tweener: following a path

// forked from makc3d's forked from: Tweener: following a path
// forked from makc3d's Tweener: following a path
package {
    import caurina.transitions.Tweener;
    import caurina.transitions.properties.CurveModifiers;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.geom.ColorTransform;
    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;
        private var bd:BitmapData;
        private var ct:ColorTransform;
        private var line:Sprite;

        private var last_x:Number;
        private var last_y:Number;

        private function lineDrawer (e:*):void {
            line.graphics.clear ();
            line.graphics.lineStyle (1, 0xFF0000);
            line.graphics.moveTo (last_x, last_y);
            line.graphics.lineTo (  mc.x,   mc.y);
            bd.draw (line);
            last_x = mc.x;
            last_y = mc.y;
            bd.colorTransform (bd.rect, ct);
        }

        public function TweenerPath () {
            // bitmap stuff
            bd = new BitmapData (465, 465, false, 0xFFFFFF);
            ct = new ColorTransform (1, 1, 1, 1, 1, 1, 1);
            addChild (new Bitmap (bd));
            line = new Sprite;

            addEventListener ("enterFrame", lineDrawer);

            // init Tweener
            CurveModifiers.init();

            // make 20 random checkpoints
            var i:int;
            for (i = 0; i < 25; i++) {
                mc = new Sprite;
                mc.graphics.beginFill (0xFF00);
                mc.graphics.drawCircle (0, 0, 3);
                mc.graphics.endFill ();
                mc.x = 50 + 350 * Math.random ();
                mc.y = 50 + 350 * Math.random ();
                if (i == 0) {
                    last_x = mc.x;
                    last_y = mc.y;
                }
                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),
                onUpdate: centerAtMC,
                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),
                onUpdate: centerAtMC,
                time:30, onComplete:goForward, transition:"linear"});
        }

        private function centerAtMC ():void
        {
            x = 465/2 - mc.x;
            y = 465/2 - mc.y;
        }

        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;
        }
    }
}