2013-01-09 96 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

擺脫了錯誤,但現在沒有動畫發生。但是,這可能部分歸因於我如何定義呼叫轉移,因此超出了此問題的範圍。錯誤消失了! –