window.requestAnimationFrame困扰

时间:2014-02-01 23:22:51

标签: javascript debugging

我一直在重载callstack和获取“参数1不是函数”错误,试图让window.requestAnimationFrame正常工作。下面的代码在trywindow.requestAnimationFrame(gameloop(“running”))上给出了“参数1不是函数”错误:

function gameloop(state,timestamp){
  if(state=="running"){
    elapsed=timestamp-start;
    frame=timestamp-lasttime;  //this is for performance calculations and animations.
    lasttime=timestamp;
    //calculate();
    //quaddetect();
    //raydetect();
    //animate();
  }
  else if(state=="paused"){
    //menu();
    //animate();
  }
}
start=window.performance.now();lasttime=start;
window.requestAnimationFrame(gameloop("running"));

下面的代码给了我一个callstack溢出错误(非常肯定我只是误解了window.requestAnimationFrame这个):

var lasttime=window.performance.now();

function gameloop(state,timestamp){
  if(state=="running"){
    elapsed=timestamp-start;
    frame=timestamp-lasttime;  //this is for performance calculations and animations.
    lasttime=timestamp;
    //calculate();
    //quaddetect();
    //raydetect();
    //animate();
    window.requestAnimationFrame(gameloop("running"));
  }
  else if(state=="paused"){
    animate();
    window.requestAnimationFrame(gameloop("running"));
  }
}
start=window.performance.now();
lasttime=start;
window.requestAnimationFrame(gameloop("running"));

问题是两部分:为什么游戏循环(“运行”)没有被识别为函数,我将如何以正确的方式使用window.requestAnimationFrame?

0 个答案:

没有答案