2013-01-09 97 views
1

我工作很短的“发动机”类有所简化与three.js所交互传递。我重构的例子的Render()方法来此引擎JS类,如下:功能不是作为一个功能

var Engine = function() { 
    var pub = {}, 
     scene, 
     camera; 

    // SNIP: Extra private and public variables and functions... 

    pub.Render = function (fixedUpdate) { 
    requestAnimationFrame(pub.Render); 
    fixedUpdate(); 
    renderer.render(scene, camera); 
    }; 

    return pub; 
}(); 

我试图通过传递一些函数,我要执行它的操作使用它。例如:

Engine.Render(function() { 
    cube.rotation.x += 0.01; 
    cube.rotation.y += 0.02; 
}); 

当我尝试在Chrome上运行这一点,但是,我得到的错误:遗漏的类型错误:数量不是功能,并在检查Engine.Render的fixedUpdate变量,它通常是一些非常。大量的,显然不登记为调用代码传递的匿名函数

作为一个理智的测试,我尝试的句柄传递到非匿名函数,像这样:

function animation() { 
    cube.rotation.x += 0.01; 
    cube.rotation.y += 0.02; 
} 

Engine.Render(animation); 

...并得到完全相同的结果。我怀疑问题在于我如何调用参数。

+3

的大量是时间戳记,传递给requestAnimationFrame'的'回调在运行时。可能相关:http://stackoverflow.com/questions/12942722/typeerror-canvas-getcontext-is-not-a-function/ – apsillers

回答

3

试试这个:

pub.Render = function (fixedUpdate) { 
    requestAnimationFrame(function() {pub.Render();}); 
    fixedUpdate(); 
    renderer.render(scene, camera); 
    }; 
+0

不要你的意思'函数(){pub.Render()}'? :-P –

+0

是我没有..感谢 – Hogan

+0

摆脱了错误,但现在没有动画发生。但是,这可能部分归因于我如何定义呼叫转移,因此超出了此问题的范围。错误消失了! –