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

标签 javascript html canvas

所以我的问题是球只是穿过砖 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/

相关文章:

HTML 和 CSS : using background image as a clickable link

javascript - 防止 HTML 5 `video` 在 src 更改时闪烁海报图像

Java Canvas 不会绘画

javascript - 如何检测透明 Canvas 上的形状?

javascript - 未捕获的类型错误 : Cannot set property of 'fillStyle' of null HTML5 Javascript

javascript - 为什么我的捕获括号之外的字符包含在我的正则表达式的匹配中?

javascript - 如何动态填充我的 ChartJS 饼图

JavaScript 多对象实例,无法正确访问事件处理程序中的属性

javascript - 让 ngClick 处理动态字段

javascript - JQuery - 选择选项不会附加在更改事件上