RSS

Velocity Animation with Actionscript 3

03 Jul
Velocity Animation
package {
    import flash.display.Sprite;
    import flash.events.Event;
public class Main extends Sprite {
        private var _sprite:Sprite = new Sprite(  );
public function Main(  ) {
            _sprite.graphics.beginFill(0x0000ff100);
            _sprite.graphics.drawCircle(0025);
            _sprite.graphics.endFill(  );
            _sprite.x = 50;
            _sprite.y = 10;
            addChild(_sprite);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        
        public function onEnterFrame(event:Event):void {
        _sprite.x += 1;
        _sprite.y += 2;
        }
    }    
}  

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

—————————-Change speed direction after hitting the boundary  —————————–

package {
     import flash.display.Sprite;
     import flash.events.Event;
     import flash.geom.ColorTransform;
     import flash.geom.Rectangle;
     
     public class Main extends Sprite {
          public var speedX:int 10;
          public var speedY:int = –10;

          public function Main() {
               addEventListener(Event.ENTER_FRAME, onEnterFrame);
               graphics.beginFill(0xFF1);
               graphics.drawCircle(0025);
               graphics.endFill();

               var colorTransform:ColorTransform = new ColorTransform();
               colorTransform.color = Math.random()*0xFFFFFF;
               transform.colorTransform = colorTransform;
          }
          
          private function onEnterFrame(event:Event):void {
               x += speedX;
               y += speedY;

               var bounds:Rectangle = getBounds(parent);

               if (bounds.left < || bounds.right > stage.stageWidth) {
                    speedX *= –1;
               }
               if (bounds.top < || bounds.bottom > stage.stageHeight) {
                    speedY *= –1;
               }
          }
     }
}

Click here for More detail

Advertisements
 
Leave a comment

Posted by on July 3, 2011 in Action Script3

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: