2017-04-06 5 views
0

私はゲームを書き込もうとしていると私は、関数の上映プレーヤーでの問題だ:私は、関数を呼び出したいとき呼び出す機能の悪い引数

showFurry=function(){ 
    this.x = Math.floor(Math.random() * 10); 
    this.y = Math.floor(Math.random() * 10); 
    this.board[ this.position(this.furry.x , this.furry.y) ].classList.add('furry'); 
} 

とコンソルで:

function Furry() { 
    this.board=document.querySelectorAll('#board div'); 
    this.x = 0; 
    this.y = 0; 
    this.direction = "right"; 
} 

とを

Uncaught TypeError: Cannot read property 'x' of undefined

https://jsfiddle.net/mdx3w24c/

:私はこれを得ましたこの状態で

機能showFurryとshowCoinを呼び出した後、私はこれを受ける必要があります。 enter image description here

+1

この小さな情報で誰もあなたを助けることはできませんどこから/いつ/なぜshowFurryが呼び出されているか、それはもっと大きなものの一部ですか、これはコンテキスト/ this ...などです。plsは完全なコード、jsbin/fiddleも大いに役立つでしょう;) – MarcelD

+0

'this.furry.x'ではなく' this.x'を意味しますか? 'y'と同じ – bansi

答えて

0

が機能showFurryshowCoinを削除してください。次に、これらのことを行うGameクラスの関数を作成します。あなたがゲームを起動するとき

Game.prototype.start = function() { 
    this.board[this.position(this.coin.x, this.coin.y)].classList.add('coin'); 
    this.board[this.position(this.furry.x, this.furry.y)].classList.add('furry'); 
}; 

その後、代わりにshowFurryshowCoinを呼び出すあなたはGame.start()を呼び出すことができます。

var game = new Game(); 
game.start(); 

また、あなたのコインのコンストラクタは、x & yにランダムな値を設定しますが、毛皮のような構造は、その後、0にそれらの両方を設定しshowFurry機能でランダムな値に設定します。 thisフィドルでは、私はコンストラクタに移動しました。