Best way for simple game-loop in Javascript?

前端 未结 8 569
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 01:51

Is there a simple way to make a game loop in JavaScript? something like...

onTimerTick() {
  // update game state
}
相关标签:
8条回答
  • 2020-12-14 02:52

    Yep. You want setInterval:

    function myMainLoop () {
      // do stuff...
    }
    setInterval(myMainLoop, 30);
    
    0 讨论(0)
  • 2020-12-14 02:53

    Would this do?

    setInterval(updateGameState, 1000 / 25);
    

    Where 25 is your desired FPS. You could also put there the amount of milliseconds between frames, which at 25 fps would be 40ms (1000 / 25 = 40).

    0 讨论(0)
提交回复
热议问题