Chapter 39 Example 3

by actionscriptbible
♥0 | Line 40 | Modified 2010-02-10 02:47:01 | MIT License
play

ActionScript3 source code

/**
 * Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/4E54
 */

package {
  import flash.display.*;
  import flash.events.Event;
  import flash.text.*;
  import flash.utils.getTimer;
  public class ch39ex3 extends Sprite {
    protected const SPEED:Number = 50 / 1000;
    //50 pixels per second * 1 sec / 1000 ms = 50/1000 pixels per millisecond
    protected var ball:DisplayObject;
    protected var tf:TextField;
    protected var lastTime:int;
    public function ch39ex3() {
      ball = new Ball();
      ball.y = stage.stageHeight/2;
      addChild(ball);
      tf = new TextField(); tf.width = 0; tf.height = 14; tf.x = tf.y = 5;
      tf.autoSize = TextFieldAutoSize.LEFT;
      tf.backgroundColor = 0; tf.background = true;
      tf.defaultTextFormat = new TextFormat("_typewriter", 11, 0xffffff);
      addChild(tf);
      addEventListener(Event.ENTER_FRAME, onEnterFrame);
      lastTime = getTimer();
    }
    protected function onEnterFrame(event:Event):void {
      //mess things up by changing the framerate randomly
      stage.frameRate = Math.random() * 45 + 1;
      
      var time:int = getTimer();
      var dt:Number = time - lastTime;
      ball.x += SPEED * dt;
      lastTime = time;

      tf.text = stage.frameRate.toFixed() + " fps";
      if (ball.x > stage.stageWidth) ball.x = 0;
    }
  }
}
import flash.display.Shape;
class Ball extends Shape {
  public function Ball(color:uint = 0xff0000, size:Number = 50) {
    graphics.beginFill(color);
    graphics.drawCircle(0, 0, size);
  }
}