[英]Calling function bad argument
我正在嘗試編寫游戲,但顯示播放器的功能出現問題:
function Furry() {
this.board=document.querySelectorAll('#board div');
this.x = 0;
this.y = 0;
this.direction = "right";
}
和:
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');
}
在consol中,當我想調用函數時,我得到了這個:
未捕獲的TypeError:無法讀取未定義的屬性“ x”
https://jsfiddle.net/mdx3w24c/
在此狀態下,調用函數showFurry和showCoin之后,我應該收到以下消息:
嘗試刪除功能showFurry
和showCoin
。 然后,在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');
};
然后,當您開始游戲時,可以調用Game.start()
而不是調用showFurry
和showCoin
;
var game = new Game();
game.start();
同樣,您的Coin構造函數將隨機值設置為x&y,但是毛茸茸的構造將它們都設置為0,然后在showFurry
函數中將它們設置為隨機值。 在這個小提琴中,我將其移到了構造函數中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.