我似乎无法使用箭头功能。
我的问题是,当我使用箭头函数时,我无法计算矢量数学。
你能帮我解决共享代码的#move函数中的矢量数学问题吗?
我已经正确地实现了箭头功能,我需要帮助的代码在#move 中。
class Vehicle {
constructor(x, y, width, height, color) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
(this.location = new Vector2d(this.x, this.y)),
(this.acceleration = new Vector2d(0.2, 0.2)),
(this.velocity = new Vector2d(0, 1)),
(this.maxSpeed = 5);
this.minSpeed = 1;
this.angle = 0;
this.color = color;
this.controls = new Controls();
}
update() {
this.#move;
}
#move() {
if (this.controls.forward) {
this.velocity.set(0, 1);
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
this.location.add(this.velocity);
}
if (this.controls.reverse) {
this.velocity.set(0, -1);
this.velocity.sub(this.acceleration);
this.velocity.limit(this.minSpeed);
this.location.add(this.velocity);
}
while (this.velocity > 0) {
if (this.controls.left) this.angle += 0.03;
if (this.controls.right) this.angle -= 0.03;
}
this.x -= Math.sin(this.angle) * this.velocity.x;
this.y -= Math.cos(this.angle) * this.velocity.y;
}
draw(ctx) {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(-this.angle);
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.rect(-this.width / 2, -this.height / 2, this.width, this.height);
ctx.fill();
ctx.restore();
}
}