ale*_*lex 2 javascript canvas pong
我正在尝试使用带有 JavaScript 的画布来制作 Pong。
我已经很多年没有制作任何游戏了,因此我还是一个初学者。
我遇到一个问题,我正在尝试移动球(目前只是其 x 位置)并尝试删除其先前的位置。我使用的代码适用于桨(向上和向下键移动它)。
但是,它似乎并不想与球一起工作。
我究竟做错了什么?
this.draw = function() {
ctx.clearRect(this.prevX - this.radius, this.prevY - this.radius, this.radius * 2, this.radius * 2);
ctx.fillStyle = this.color;
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
ctx.fill();
this.prevX = this.x;
this.prevY = this.y;
}
Run Code Online (Sandbox Code Playgroud)
我知道我正在使用clearRect(),但我的印象是它只是删除了画布的矩形部分。
clearRect(x,y,width,height):清除指定区域并使其完全透明
来源。
请随意给我任何其他提示,因为我在这方面几乎是初学者。
实际上是因为你没有打电话beginPath,所以你画的每个新圆圈也会重画所有旧的圆圈!
修复此处并附有插图:
拿出电话beginPath看看之前发生了什么。