【问题标题】:Can't stop requestAnimationFrame (Javascript/html5: canvas)无法停止 requestAnimationFrame (Javascript/html5: canvas)
【发布时间】:2013-10-27 06:30:25
【问题描述】:

我在退出/杀死/退出我的 requestAnimationFrame 时遇到问题。我的游戏循环工作,我可以暂停我的游戏(游戏循环保持活跃)。但是当我想返回我的菜单时,我想杀死 requestAnimationFrame,所以它停止绘制和更新。我在互联网和stackoverflow上搜索过,发现类似的问题尝试了响应,但没有运气:(

var fps = 60;
var now;
var then = Date.now();
var interval = 1000/fps;
var delta;  

//============================================================================

function gameloop(){
window.requestAnimationFrame(gameloop);

if (game.isUnpaused()){
//game is not paused, update all
      now = Date.now();
          delta = now - then;

              if (delta > interval) {
                  then = now - (delta % interval);
              //DO ALL WHAT'S NEEDED: draw avatar,move obstacles,move avatar....
                  }}

//what to do when game is paused:
else{//draw stuff when game is paused}

}

有人可以帮帮我吗?

提前致谢

【问题讨论】:

标签: html loops canvas requestanimationframe


【解决方案1】:
var active = true;
function gameloop(){
    if(active){
        window.requestAnimationFrame(gameloop);
        }
    }

【讨论】:

  • 非常感谢 :) 你我的英雄 ;)
猜你喜欢
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 2014-08-14
  • 2013-09-30
  • 2020-02-27
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
相关资源
最近更新 更多