2016-05-05 14 views
0

私は関数内でループメソッドを呼び出していますが、問題は "ループ"を返す "この関数は、」ここに例を示します"this" returnを使ったプロトタイプ呼び出し関数は "関数ではありません"

window.onload= startGame(); 

var theGame; 

function startGame(){ 
    theGame = new game(); 
    theGame.loop(); /*im calling the method*/ 

} 

function game(){ 
    this.canvas=document.getElementById("breakout"); 
    this.context = this.canvas.getContext('2d'); 
    this.canvas.width=window.innerWidth; 
    this.canvas.height=window.innerHeight; 
    // this.loop(); /*¿can i call it here too?*/ 
    this.speed = 100; 

} 

game.prototype.loop = function() { 

    console.log(this.speed+=1); 

    var self = this; 
    requestAnimationFrame(function(){ 
      self.loop(); 
     }); 

}; 

私が呼ぶとき 『』ブラウザのエラー方法 『LOOPをキャッチされない例外TypeError:theGame.loopは関数ではありません』

+1

それは 'window.onload = startGameにする必要がある;'、そこに機能を呼び出すことなく。 – Bergi

+2

機能を早く呼び出すことに関連する問題に関する質問は何度も尋ねられています。私はこのために標準的なQ&Aが必要だと思っています。 –

+0

'game.prototype.loop'を定義した後、' startGame() 'を最後まで移動してください。 – Oriol

答えて

2

問題は、あなたがしていることです単純にrの代わりに関数をすぐに呼び出すので、onloadイベントハンドラを正しく設定しないそれをeferencingする。

window.onload= startGame(); 

は次のようになります。

window.onload = startGame; 
関連する問題