2
我学习JS,我创建了一个类Entity
这样的:JS扩展构造类
class Entity {
constructor(x=0, y=0, dx=0, dy=0, width=50, height=50, solid=false,
color="black", name="entity", id=Math.random()) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.width = width;
this.height = height;
this.solid = solid;
this.color = color;
this.name = name;
this.id = id;
entityList[id] = this;
}
UpdatePosition() {
this.x += this.dx;
this.y += this.dy;
}
Draw() {
ctx.save();
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
ctx.restore();
}
BorderCollision() {
if (this.solid == true) {
if (this.x <= 0) {
this.dx = -this.dx;
}
if (this.x + this.width >= canvas.width) {
this.dx = -this.dx;
}
if (this.y <= 0) {
this.dy = -this.dy;
}
if (this.y + this.height >= canvas.height) {
this.dy = -this.dy;
}
}
}
EntityUpdate() {
this.UpdatePosition();
this.Draw();
this.BorderCollision();
}
}
而现在,我要扩展这个类中新建一个名为Player
谁拥有了新的成员:canMove
但我不知道该怎么做一个新的构造原因,当我写constructor(canMove) {this.canMove = canMove; +}
我得到一个错误:(
谢谢)!
这里是一个很好的解释 - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes – Pugazh
谢谢,我这样做: 姑娘播放器扩展实体 { \t构造(canMove) \t { \t \t super.constructor(); \t \t this.canMove = canMove; \t}} ,我得到了一个新的错误: “未捕获的ReferenceError:这是不是definedPlayer @的index.html:84(匿名函数)@ index.html的:145” 再次 感谢您帮助我;) –
@ AlexandreDaubricourt'super.constructor' ???它需要是'super()' – Bergi