Calculating position based on velocity : Velocity : Animation Flash / Flex / ActionScript examples


Flash / Flex / ActionScript examples » Animation » Velocity »

 

Calculating position based on velocity


 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.utils.*;
  public class Main extends Sprite {
    private var distancePerSecond:int 50;  // Pixels to move per second
    private var now:int;                     // The current time
    private var then:int;                    // The last screen-update time
    private var circle:Shape;                // The object to animate

    public function Main(  ) {
      circle = new Shape(  );
      circle.graphics.beginFill(0x0000FF1);
      circle.graphics.lineStyle(1);
      circle.graphics.drawEllipse(002525);
      addChild(circle);

      then = getTimer(  );
      now  = then;

      addEventListener(Event.ENTER_FRAME, enterFrameListener);
    }

    private function enterFrameListener (e:Event):void {
      then = now;
      now = getTimer(  );
      var elapsed:int = now - then;
      var numSeconds:Number = elapsed / 1000;

      var moveAmount:Number = distancePerSecond * numSeconds;


      circle.x += moveAmount;
    }
  }
}

        



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


Flash / Flex / ActionScript examples

 Navioo Animation
» Velocity