【发布时间】:2019-06-01 14:41:25
【问题描述】:
我正在尝试用随机移动的球制作画布背景。球到达边缘时会反弹。这应该很容易,但显然我错过了一些东西。
现在,前几秒看起来还不错,但过了一会儿,这些球就懒得越过左边界,也永远不会反弹回来。这几天我一直在想办法,但失败了。任何帮助表示赞赏。
update(delta, canvas) {
let deltaX = delta * Math.cos(this.movingDirection * Math.PI / 180);
let deltaY = delta * Math.sin(this.movingDirection * Math.PI / 180);
this.axisX += deltaX;
this.axisY += deltaY;
//set border
if (this.axisX > (canvas.width)) {
if (this.movingDirection > 270 && this.movingDirection < 360) {
this.movingDirection = 180 + this.movingDirection;
} else if (this.movingDirection < 90 && this.movingDirection > 0) {
this.movingDirection = 180 - this.movingDirection;
}
}
if (this.axisX < 0) {
if (this.movingDirection > 180 && this.movingDirection < 270) {
this.movingDirection = 540 - this.movingDirection;
} else if (this.movingDirection <= 180 && this.movingDirection > 90) {
this.movingDirection = 180 - this.movingDirection;
}
}
if (this.axisY > (canvas.height) || this.axisY < 0) {
if (this.movingDirection > 180 ) {
this.movingDirection = 360 - this.movingDirection;
} else if (this.movingDirection <= 180) {
this.movingDirection = 360 - this.movingDirection;
}
}
this.draw();
}
this.movingDirection 是一个在 0 到 360 之间随机生成的数字。
这是一个完整的例子https://jsfiddle.net/calmdown/qr89b034/1/
谢谢!
【问题讨论】:
标签: canvas html5-canvas