gpt4 book ai didi

javascript - 弹球破砖游戏,砖 block 弹不起来

转载 作者:行者123 更新时间:2023-12-02 18:03:06 27 4
gpt4 key购买 nike

所以我的问题是球只是穿过砖 block ,什么也没有发生。我将把整个代码放在jsbin中,这里我只发布不起作用的部分。正如你所看到的,我有一个弹跳球、一个 Racket 、砖 block 和一个animationFrame函数。

for (var i in bricks) {
if (!bricks[i].isDestroyed) {

if (
(this.y + this.radius == bricks[i].y - bricks[i].height)
&& (this.x >= bricks[i].x)
&& (this.x <= bricks[i].x + bricks[i].width)
) {
bricks[i].isDestroyed = true;
destroyedBrick = true;
this.direction.y = "up";
ctx.clearRect(bricks[i].x, bricks[i].y, bricks[i].width, bricks[i].height);
}

if (
(this.y - this.radius == bricks[i].y)
&& (this.x >= bricks[i].x)
&& (this.x <= bricks[i].x + bricks[i].width)
) {
bricks[i].isDestroyed = true;
destroyedBrick = true;
this.direction.y = "down";
ctx.clearRect(bricks[i].x, bricks[i].y, bricks[i].width, bricks[i].height);
}

if (
(this.x - this.radius == bricks[i].x)
&& (this.y - this.radius <= bricks[i].y)
&& (this.y - this.radius >= bricks[i].y - bricks[i].height)
) {
bricks[i].isDestroyed = true;
destroyedBrick = true;
this.direction.x = "left";
ctx.clearRect(bricks[i].x, bricks[i].y, bricks[i].width, bricks[i].height);
}

if (
(this.x == bricks[i].x + bricks[i].width)
&& (this.y - this.radius <= bricks[i].y)
&& (this.y - this.radius >= bricks[i].y - bricks[i].height)
) {
bricks[i].isDestroyed = true;
destroyedBrick = true;
this.direction.x = "right";
ctx.clearRect(bricks[i].x, bricks[i].y, bricks[i].width, bricks[i].height);
}
}
}

Jsbin代码:http://jsbin.com/eZOxusaQ/1/watch?html,js,output

最佳答案

当您可能需要不等式时,冲突代码似乎在某些检查中使用了 == 。所以我的猜测是球正在跨过那个确切的位置,导致其他检查被跳过。根据需要将所有比较切换为 >= 或 <= 并查看是否可以解决问题。

关于javascript - 弹球破砖游戏,砖 block 弹不起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20338709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com