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

flash on 2013-6-7

experiment: travelling on a spline path with constant speed
Get Adobe Flash player
by mutantleg 07 Jun 2013
/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/1Ek9
 */

package {
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        
        public function FlashTest() {
            
            
      
             
             graphics.lineStyle(2, 0);
             
             x0 = 200;
             y0 = 100;
             x1 = 500;
             y1 = 200;
             x2 = 150;
             y2 = 300;
             
             graphics.drawCircle(x0, y0, 8);
             graphics.drawCircle(x1, y1, 8);
             graphics.drawCircle(x2, y2, 8);
            
            
            var kx:Number;
            var ky:Number;
            
            graphics.lineStyle(2,0,0.2);
            
            for (u = -1; u < 2.0; u += 0.1)
            {
                kx = getBez(u, x0, x1, x2);
                ky = getBez(u, y0, y1, y2);
                
                graphics.drawCircle(kx, ky, 8);
            }//nextu
            
            
            cx = x0;
            cy = y0;
            vx = 0;
            vy = 1;
            
            var i:int;
          
            var dx:Number;
            var dy:Number;
            var mag:Number;
            
            var px:Number;
            var py:Number;
            
            var speed:Number;
            
            speed = 32; // 128; //64; // 32; //16; //4;
            
            px = cx;
            py = cy;
            
            u = 0;
            mx = getBez(u, x0, x1, x2);
            my = getBez(u, y0, y1, y2);
            
            vx = mx - cx;
                vy = my - cy;
            graphics.lineStyle(2, 0);
            
            for (i = 0; i < 200 ;i++)
            {
            
                if ( Math.abs(mx-cx)<=speed && Math.abs(my-cy)<=speed)
                {
                  //u+=0.05;
                  u += 0.1;
                  mx = getBez(u, x0, x1, x2);
                  my = getBez(u, y0, y1, y2);
                  graphics.drawCircle(mx,my,8);
                  
                  dx = mx -cx;
                  dy = my -cy;
                  mag= Math.sqrt(dx*dx+dy*dy);
                  //if (mag == 0) {mag = 0.01;}
                  if (mag < speed) { continue; }
                  vx = dx / mag*speed;
                  vy = dy / mag*speed;
                  
                     //vx = (mx - cx) *0.2;
                     //vy = (my - cy) *0.2;
                }
                
                cx += vx;
                cy += vy;
                
                graphics.drawCircle(cx, cy, 4);
                
                graphics.moveTo(px,py);
                graphics.lineTo(cx, cy);
                
                px = cx;
                py = cy;
            }//nexti
              
            u = 0;
            mx = getBez(u, x0, x1, x2);
            my = getBez(u, y0, y1, y2);
            cx = x0;
            cy = y0;
            vx = 0;
            vy = 1;
            
            
            ps = new Sprite();
            ps.graphics.beginFill(0, 0.5);
            ps.graphics.drawCircle(0,0,12);
            ps.graphics.endFill();
            addChild(ps);
            
            stage.addEventListener(Event.ENTER_FRAME, onEnter);
        }//ctor
        
        public var ps:Sprite;
         
         public  var cx:Number;
         public  var cy:Number;
         public  var vx:Number;
         public  var vy:Number;
         
         public var x0:Number;
         public var y0:Number;
         public var x1:Number;
         public var y1:Number;
         public var x2:Number; 
         public var y2:Number;
         
         public var u:Number;
        
         public var mx:Number;
         public var my:Number;
            
    
        public function onEnter(e:Event):void
        {
         
          if (u > 1.1)
                {
                     u = 0;    
                     
                    mx = getBez(u, x0, x1, x2);
                    my = getBez(u, y0, y1, y2);
                    cx = x0;
                    cy = y0;
                    vx = 0;
                    vy = 1;
                }
                     
             var dx:Number;
            var dy:Number;
            var mag:Number;
             
             var speed:Number;
            
            //speed = 8;
            speed = 4; 
             
              if ( Math.abs(mx-cx)<=speed && Math.abs(my-cy)<=speed)
                {
                  //u+=0.05;
                  u += 0.1;
                  mx = getBez(u, x0, x1, x2);
                  my = getBez(u, y0, y1, y2);
                  
                  dx = mx -cx;
                  dy = my -cy;
                  mag= Math.sqrt(dx*dx+dy*dy);
                  //if (mag == 0) {mag = 0.01;}
                  if (mag < speed) { return; }
                  vx = dx / mag*speed;
                  vy = dy / mag*speed;
                  
                     //vx = (mx - cx) *0.2;
                     //vy = (my - cy) *0.2;
                     
                    
                }
                
                cx += vx;
                cy += vy;
            
            ps.x = cx;
            ps.y = cy;
            
            
        }//onenter
        
        
        
        public function getBez(t:Number, x1:Number, x2:Number, x3:Number):Number
        {
            var ta:Number = (1 - t);
            //(1-t)2 = (1-t)(1-t)        // P(t) = (1-t)2 *P0  +  2(1-t)*t*P1 + t2*P2
                return ( (ta * ta) * x1 + 2 * (ta) * t * x2 + t * t * x3 );
        }//getbezier

    }//classend
}