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

ゲームループのテスト

ActionScriptの勉強を始めたばかり。まずはゲームループを作ってみる。フレームレートをいじったり...
Get Adobe Flash player
by irgaly 12 Jun 2011
    Embed
/**
 * Copyright irgaly ( http://wonderfl.net/user/irgaly )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/je4D
 */

package {
    import flash.text.*; // TextField
    import flash.display.Sprite; // Sprite
    import flash.events.Event; // Event.ENTOR_FRAME
    import flash.utils.getTimer; // getTimer()
    public class GameLoopTest extends Sprite {
        private var _counter:TextField;
        private var _count:uint;
        private var _rate:TextField;
        private var _rateTick:int;
        public function GameLoopTest() {
            // 初期化はここかな?
            
            // フレームカウンタの設置
            _count = 0;
            _counter = new TextField();
            _counter.autoSize = TextFieldAutoSize.LEFT;
            addChild(_counter);
            
            // FPSメータの設置
            var firstFPS:TextField = new TextField();
            firstFPS.y = 20;
            firstFPS.autoSize = TextFieldAutoSize.LEFT;
            firstFPS.text = "FPSのデフォルト値: " + stage.frameRate;
            addChild(firstFPS);
            
            stage.frameRate = 60;
            _rateTick = getTimer();
            _rate = new TextField();
            _rate.autoSize = TextFieldAutoSize.LEFT;
            _rate.y = firstFPS.y + 20;
            addChild(_rate);
            
            addEventListener(Event.ENTER_FRAME, loop);
        }
        private function loop(e:Event):void {
            countUp();
            updateFPS();
        }
        private function countUp():void {
            _counter.text = (++_count).toString();
        }
        private function updateFPS():void {
            var oldTick = _rateTick;
            _rateTick = getTimer();
            if (_count % (stage.frameRate/2) == 0){
              _rate.text = "FPS: " + (1000/(_rateTick - oldTick)).toFixed(2) + " / " + stage.frameRate;
            }
        }
    }
}