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

Guide

Get Adobe Flash player
by jozefchutka 07 May 2010
/**
 * Copyright jozefchutka ( http://wonderfl.net/user/jozefchutka )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/frat
 */

package
{
    import __AS3__.vec.Vector;
    
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    
    import net.hires.debug.Stats;
    
    [SWF(width="465", height="465", frameRate="60", backgroundColor="#FFFFFF")]
    
    public class GiudeApp extends Sprite
    {
        private var car1:Car = new Car();
        private var car2:Car = new Car();
        
        public function GiudeApp()
        {
            var guide:Guide = new Guide();
            guide.add(200, 200);
            guide.add(400, 400);
            guide.add(200, 300);
            
            car1.guide = guide;
            car2.guide = guide.copy();
            
            addChild(car1);
            addChild(car2);
            addChild(new Stats());
            
            stage.addEventListener(Event.ENTER_FRAME, enterFrame);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
        }
        
        private function enterFrame(event:Event):void
        {
            car1.move(3);
            car2.move(6);
            
            graphics.clear();
            graphics.lineStyle(0, 0x00ff00);
            graphics.moveTo(car1.guide.firstPoint.x, car1.guide.firstPoint.y);
            for each(var point:Point in car1.guide.points)
                graphics.lineTo(point.x, point.y);
        }
        
        private function mouseDown(event:MouseEvent):void
        {
            car1.guide.add(mouseX, mouseY);
            car2.guide.add(mouseX, mouseY);
            graphics.lineTo(mouseX, mouseY);
            
            stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
            stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
        }
        
        private function mouseMove(event:MouseEvent):void
        {
            car1.guide.add(mouseX, mouseY);
            car2.guide.add(mouseX, mouseY);
            graphics.lineTo(mouseX, mouseY);
        }
        
        private function mouseUp(event:MouseEvent):void
        {
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
            stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
        }
    }
}

import __AS3__.vec.Vector;

import flash.display.Shape;
import flash.geom.Point;

class Guide extends Object
{
    public var points:Vector.<Point> = new Vector.<Point>();
    
    public function Guide():void
    {
    }
    
    public function add(x:Number, y:Number):void
    {
        points.push(new Point(x, y));
    }
    
    public function shift():void
    {
        points.shift()
    }
    
    public function get firstPoint():Point
    {
        return points.length ? points[0] : null;
    }
    
    public function get nextPoint():Point
    {
        return points.length > 1 ? points[1] : null;
    }
    
    public function copy():Guide
    {
        var guide:Guide = new Guide();
        for each(var point:Point in points)
            guide.add(point.x, point.y);
        return guide;
    }
}

class Car extends Shape
{
    private var _guide:Guide;
    
    public function Car():void
    {
        graphics.beginFill(0xff0000, 0.8);
        graphics.drawCircle(0, 0, 5);
        graphics.endFill();
    }
    
    public function set guide(value:Guide):void
    {
        _guide = value;
        x = guide.firstPoint.x;
        y = guide.firstPoint.y;
    }
    
    public function get guide():Guide
    {
        return _guide;
    }
    
    public function move(speed:Number):void
    {
        var p0:Point = guide.firstPoint;
        var p1:Point = guide.nextPoint;
        if(!p1)
            return;
        
        var distance:Number = Point.distance(p0, p1);
        var coeff:Number = Math.min(speed / distance, 1);
        p0.x = x = p0.x + (p1.x - p0.x) * coeff;
        p0.y = y = p0.y + (p1.y - p0.y) * coeff;
        if(p0.x == p1.x && p0.y == p1.y)
            guide.shift();
            
        if(guide.nextPoint && speed > distance)
            move(speed - distance);
    }
}