FPSの測定

by _perfect
ENTER_FRAMEイベント中で回数をカウントして描画回数を調べる。
♥0 | Line 34 | Modified 2010-10-07 14:39:45 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.Event;
    import flash.utils.getTimer;
    
    public class FlashTest extends Sprite {
        private var draw_count:int;
        public var old_timer:uint;
        private var update_time:uint;
        private var fps:uint;
        
        public function FlashTest() {
            // write as3 code here..
            draw_count = 0;
            update_time = 1000;//表示が変わるまでの時間
            old_timer = getTimer();
            
            var tf:TextField = new TextField();
            tf.background = true;
            tf.text = "FPSを測定を開始します";
            tf.width = 300;
            tf.height = 2000;
            addChild(tf);
            
            this.addEventListener(Event.ENTER_FRAME ,doTimer);
            
            function doTimer():void{
                draw_count += 1;
                // 1秒経過していれば
                if (getTimer()-old_timer >= update_time) {
                    // 少数第1位まで表示
                    fps = draw_count * 1000 / (getTimer() - old_timer);
                    fps = Math.floor(fps * 10) / 10;
                    tf.text = "FPSを測定"+"\n" + fps + ' / ' + stage.frameRate;
                    old_timer = getTimer();
                    draw_count = 0;
                }
            }
            
        }
    }
}