LoginSignup
3
3

More than 5 years have passed since last update.

JavaScriptでゲームループ

Last updated at Posted at 2012-08-06

ただのコピペです。
詳細はこちら→ http://t100life.blog121.fc2.com/blog-entry-209.html
動くテスト→ http://jsfiddle.net/T7Hug/
※consoleに出力しているだけなので、consoleみないとなにも動いてないように見えます。

var Game = {};
Game.FPS = 60;

Game.run = (function() {
    var loops = 0,
         skipTicks = 1000 / Game.FPS,

        //1秒間に実行したいロジックの回数
         maxFrameSkip = 10,

        //スキップする間隔
         nextGameTick = +new Date();

    return function {
        loops = 0;

        while (+new Date() > nextGameTick && loops < maxFrameSkip) {
            Game.update();
            nextGameTick += skipTicks;
            loops++;
        }

        //Rendering
        Game.draw();
    };
})();

//Rendering logic
Game.draw = function () {console.log('draw');};

//Game logic
Game.update = function () {console.log('update');};

// Start the game loop
Game._intervalId = setInterval(Game.run, 0);
3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3