无法停止requestAnimationFrame(Javascript / html5:canvas)

时间:2013-10-18 12:04:04

标签: html5 loops canvas requestanimationframe

我在退出/取消/退出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}

}

任何人都可以帮助我吗?

提前致谢

1 个答案:

答案 0 :(得分:2)

var active = true;
function gameloop(){
    if(active){
        window.requestAnimationFrame(gameloop);
        }
    }