2012-05-23 51 views
1
function airEngineJS (canvasArg, properties) { 

    this.canvas = (canvasArg == "undefined")? trace("Failed to set up canvas for airEngineJS") : canvasArg; 
    this.cameras = []; 
    this.displayObjects = []; 
    this.hudDisplayObjects = []; 
    this.fps = (properties == "undefined" || properties.fps == "undefined")? 30 : properties.fps; 

    if (properties == "undefined" || properties.autoinit == "undefined" || properties.autoinit == true){ 
     this.keyboard = new keyboardJS(); 
     this.keyboard.init(); 
     this.cameras.push(new airCameraJS(this)); 
    } 
    this.enterframe = setInterval(this.intervalCaller, 1000/this.fps); 
    trace("A new airEngineJS has been created"); 
    } 
    airEngineJS.prototype = { 
    intervalCaller : function() { 
     this.mainLoop(); 
    }, 
    logic : function() { 
     for (var i = 0; i < this.displayObjects.length; ++i){ 
     this.displayObjects[i].logic(); 
     } 
     for (var j = 0; j < this.cameras.length; ++j){ 
     this.cameras[j].logic(); 
     } 
    }, 
    render : function() { 
     for (var i = 0; i < this.cameras.length; ++i){ 
     for (var j = 0; j < this.displayObjects.length; ++j){ 
      this.displayObjects[j].renderOn(this.cameras[i]); 
     } 
     } 
     for (var i = 0; i < this.hudDisplayObjects.length; ++i){ 
     this.hudDisplayObjects[i].renderOn(this.canvas); 
     } 
    }, 
    mainLoop : function() { 
     this.logic(); 
     this.render(); 
    } 
    } 

的間隔內的函數內的函數區間[this.enterframe =的setInterval(this.intervalCaller,1000/this.fps);]調用正確(此。 intervalCaller),但是這會嘗試在DOM中調用(this.mainLoop())。的調用的對象,這也調用該對象

有關我應該怎麼做的任何建議? :(

回答

1

可以收,較實際的函數調用是這樣的:

var self = this; 
this.enterframe = setInterval(function() { 
    self.intervalCaller(); 
}, 1000/this.fps); 

它首先創建新實例,然後傳遞給setInterval瓶蓋內所使用的參考

+0

東西是錯誤的。在我的代碼中,它停止工作...我試圖解決它,我會告訴你,如果這工作... –

+0

@DavidDaSilvaContín你可以檢查出這個小提琴,如果你想驗證的代碼:http:// jsfiddle.net/qetAK/ –

+0

我已經使它工作了!這是一個奇怪的問題。我相信它只是通過改變「未定義」比較中的至<'>。我會檢查鏈接:) 代碼工作完美。謝謝!我相信現在我明白了:/因此,它試圖在DOM中執行,因爲路徑/路徑使用的是'this',它不再是它的對象內部,或多或少? :| –