繁体   English   中英

调用函数错误参数

[英]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之后,我应该收到以下消息: 在此处输入图片说明

尝试删除功能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');
};

然后,当您开始游戏时,可以调用Game.start()而不是调用showFurryshowCoin

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

同样,您的Coin构造函数将随机值设置为x&y,但是毛茸茸的构造将它们都设置为0,然后在showFurry函数中将它们设置为随机值。 这个小提琴中,我将其移到了构造函数中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM