Im getting this following error in my script: "Cannot read property 'moving' of undefined" on line 26 of player.js
(我在脚本中遇到以下错误:player.js第26行上的“无法读取未定义的属性'移动'”)
No idea why because I have "this.moving = false" defined in userMovement.js.
(不知道为什么,因为在userMovement.js中定义了“ this.movi??ng = false”。)
Does it maybe have to do with my controller in player.js?(可能与player.js中的控制器有关吗?)
Could someone help me figure out why?
(有人可以帮我找出原因吗?)
Thanks in advance.(提前致谢。)
player.js:
(player.js:)
var players = {};
class player {
constructor(name, hasController) {
this.pos = new position(this, 300, 300, 0, -16);
this.img = new animator(this, this.pos, "tilesets/player", 4, 4, .2);
if(hasController) this.controller = new userMovement(this, this.pos, this.img);
this.properties = [
this.pos,
this.img
];
if(hasController) this.properties.push(this.controller);
this.name = name;
players[name] = this;
objects.push(this);
}
step() {
if(this.name == client.socket.id)
client.send({
posX: this.pos.x,
posY: this.pos.y,
id: client.socket.id,
dir: this.img.realY,
isAnimating: this.controller.moving
}, "playerMovement");
}
}
userMovement.js:
(userMovement.js:)
class userMovement {
constructor(parent, pos, img) {
this.parent = parent;
this.pos = pos;
this.img = img;
this.pos.x = this.pos.x - (this.pos.x % 16/*32*/);
this.pos.y = this.pos.y - (this.pos.y % 16/*32*/);
this.speed = 3.5;
this.target = {
x: this.pos.x,
y: this.pos.y
}
this.moving = false;
this.keys = {
W: 87,
A: 65,
S: 83,
D: 68
};
}
step() {
if(keyIsDown(this.keys.W) && !this.moving) {
this.img.setY(3);
this.moving = true;
this.target.y -= 32;
}
if(keyIsDown(this.keys.A) && !this.moving) {
this.img.setY(1);
this.moving = true;
this.target.x -= 32;
}
if(keyIsDown(this.keys.S) && !this.moving) {
this.img.setY(0);
this.moving = true;
this.target.y += 32;
}
if(keyIsDown(this.keys.D) && !this.moving) {
this.img.setY(2);
this.moving = true;
this.target.x += 32;
}
if(this.moving) {
var distX = this.target.x - this.pos.x;
var distY = this.target.y - this.pos.y;
var dx = Math.sign(distX) * this.speed;
var dy = Math.sign(distY) * this.speed;
if(Math.abs(distX) <= this.speed && Math.abs(distY) <= this.speed) {
this.pos.x = this.target.x;
this.pos.y = this.target.y;
this.img.setX(0);
this.moving = false;
} else {
this.pos.x += dx;
this.pos.y += dy;
this.img.animateX();
}
}
}
}
ask by Zaedian translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…