[英]Calling function bad argument
I'm trying to write a game and I got a problem with function showing player: 我正在尝试编写游戏,但显示播放器的功能出现问题:
function Furry() {
this.board=document.querySelectorAll('#board div');
this.x = 0;
this.y = 0;
this.direction = "right";
}
and: 和:
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');
}
and in consol when I want to call function I got this: 在consol中,当我想调用函数时,我得到了这个:
Uncaught TypeError: Cannot read property 'x' of undefined 未捕获的TypeError:无法读取未定义的属性“ x”
https://jsfiddle.net/mdx3w24c/ https://jsfiddle.net/mdx3w24c/
At this state after calling function showFurry and showCoin I should receive this: 在此状态下,调用函数showFurry和showCoin之后,我应该收到以下消息:
Try removing the functions showFurry
and showCoin
. 尝试删除功能showFurry
和showCoin
。 Then, make a function in the Game class that will do those things. 然后,在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');
};
Then when you start your game, instead of calling showFurry
and showCoin
you can call Game.start()
; 然后,当您开始游戏时,可以调用Game.start()
而不是调用showFurry
和showCoin
;
var game = new Game();
game.start();
Also your Coin constructor set random values to x & y, but the furry construct set them both to 0, then set them to random values in the showFurry
function. 同样,您的Coin构造函数将随机值设置为x&y,但是毛茸茸的构造将它们都设置为0,然后在showFurry
函数中将它们设置为随机值。 In this fiddle, I've moved that to the constructor. 在这个小提琴中,我将其移到了构造函数中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.