javascript - JavaScript 多元素对象移动
问题描述
我试图移动具有不同车身元素及其属性的整个汽车对象,但所有元素都以不同的速度移动,而不是整辆车以一种速度移动。
对此的任何帮助都会很棒。
我还使用随机数值作为速度。
这是代码:
class Car {
constructor(p) {
//moving everything
this.direction = p.direction || 1;
//wheel constructor
this.x = p.x || 130;
this.y = p.y || 160;
this.wheelSize = p.wheelSize || 15;
this.WHcolour = p.WHcolour || "black";
//body constructor
this.carW = p.carW || 130;
this.carH = p.carH || 50;
this.Ccolour = p.Ccolour || "red";
//window constructor
this.windowW = p.windowW || 25;
this.windowH = p.windowH || 15;
this.Wcolour = p.Wcolour || "blue";
//car number
this.carNum = p.carNum || 1;
}
draw() {
//draw wheel 1
cxt.beginPath();
cxt.arc(this.x, this.y, this.wheelSize, 0, 2 * Math.PI);
cxt.fillStyle = this.WHcolour;
cxt.fill();
//draw wheel 2
cxt.beginPath();
cxt.arc(this.x * 1.8, this.y, this.wheelSize, 0, 2 * Math.PI);
cxt.fillStyle = this.WHcolour;
cxt.fill();
//draw body
cxt.beginPath();
cxt.rect(this.x * 0.9, this.y * 0.65, this.carW, this.carH);
cxt.fillStyle = this.Ccolour
cxt.fill();
//draw window1
cxt.beginPath();
cxt.rect(this.x * 1.615, this.y * 0.69, this.windowW, this.windowH);
cxt.fillStyle = this.Wcolour;
cxt.fill();
//draw window 2
cxt.beginPath();
cxt.rect(this.x * 1.0461, this.y * 0.69, this.windowW * 2.4, this.windowH);
cxt.fillStyle = this.Wcolour;
cxt.fill();
}
move() {
//velocity increasment
//wheels
this.x += this.direction;
//body
}
}
function createCar() {
while (cars.length < 1) {
let c = new Car({
//wheels
WHcolour: "black",
x: 130, y: 160, wheelSize: 15,
//body
carW: 130, carH: 50, Ccolour: "yellow",
//windows
windowW: 25, windowH: 15, Wcolour: "blue",
//car number
carNum: 1,
});
cars.push(c)
}
}
解决方案
您不能在运动的同一轴上进行乘法运算,您可以通过添加偏移量使零件彼此相对,因为您构建更复杂的对象乘法运算可用于您将从汽车发射的东西,例如火箭将相对于汽车的位置加速。
这是你的代码工作
class Car {
constructor(p) {
//moving everything
this.direction = p.direction || 1;
//wheel constructor
this.x = p.x || 130;
this.y = p.y || 160;
this.wheelSize = p.wheelSize || 15;
this.WHcolour = p.WHcolour || "black";
//body constructor
this.carW = p.carW || 130;
this.carH = p.carH || 50;
this.Ccolour = p.Ccolour || "red";
//window constructor
this.windowW = p.windowW || 25;
this.windowH = p.windowH || 15;
this.Wcolour = p.Wcolour || "blue";
//car number
this.carNum = p.carNum || 1;
}
draw() {
//draw wheel 1
cxt.beginPath();
cxt.arc(this.x, this.y, this.wheelSize, 0, 2 * Math.PI);
cxt.fillStyle = this.WHcolour;
cxt.fill();
//draw wheel 2
cxt.beginPath();
cxt.arc(this.x + 90, this.y, this.wheelSize, 0, 2 * Math.PI);
cxt.fillStyle = this.WHcolour;
cxt.fill();
//draw body
cxt.beginPath();
cxt.rect(this.x - 20 , this.y * 0.65, this.carW, this.carH);
cxt.fillStyle = this.Ccolour
cxt.fill();
//draw window1
cxt.beginPath();
cxt.rect(this.x + 70, this.y * 0.69, this.windowW, this.windowH);
cxt.fillStyle = this.Wcolour;
cxt.fill();
//draw window 2
cxt.beginPath();
cxt.rect(this.x - 10, this.y * 0.69, this.windowW * 2.4, this.windowH);
cxt.fillStyle = this.Wcolour;
cxt.fill();
}
move() {
//velocity increasment
//wheels
this.x += this.direction;
//body
}
}
function createCar() {
while (cars.length < 1) {
let c = new Car({
//wheels
WHcolour: "black",
x: 130, y: 160, wheelSize: 15,
//body
carW: 130, carH: 50, Ccolour: "yellow",
//windows
windowW: 25, windowH: 15, Wcolour: "blue",
//car number
carNum: 1,
});
cars.push(c)
}
}
function move() {
cxt.clearRect(0,0, 800,800)
for (i = 0; i < cars.length; i++) {
cars[i].draw()
cars[i].move()
}
}
let canvas = document.createElement("canvas");
canvas.width = canvas.height = 800;
let cxt = canvas.getContext('2d');
document.body.appendChild(canvas);
var cars = []
createCar()
setInterval(move, 100);
推荐阅读
- html - 平铺元素从我的父元素的一半开始,而不是从父元素的左侧开始
- blockchain - 从比特币区块链数据结构中提取钱包 ID(公钥)
- c++ - 使用带有向量的 boost::object_pool 时指针无效
- c# - 我应该在这里使用“使用”语句还是保持原样?
- c# - 如何有效地打破 C# 中的双循环?
- javascript - 面试编码时如何在短时间内用javascript实现队列?
- mysql - 这种僵局会如何发生?
- r - tmap facetmap 不适用于 tm_fill 但适用于 tm_symbols
- python - 使用 matplotlib 代理艺术家将图例添加到并行协调图中
- javascript - JavaScript:insertBefore 触发器“NotFoundError:找不到节点”