Javascript 弹跳球制动砖块游戏,无法弹起一块砖块

Javascript 弹跳球制动砖块游戏,无法弹起一块砖块,javascript,html,canvas,Javascript,Html,Canvas,所以我的问题是球只是穿过砖块,什么也没发生。我将把整个代码放在jsbin中,在这里我只发布不起作用的部分。如你所见,我有一个弹跳球,一个球拍,砖块和一个动画框架函数 for (var i in bricks) { if (!bricks[i].isDestroyed) { if ( (this.y + this.radius == bricks[i].y - bricks[i].height) && (th

所以我的问题是球只是穿过砖块,什么也没发生。我将把整个代码放在jsbin中,在这里我只发布不起作用的部分。如你所见,我有一个弹跳球,一个球拍,砖块和一个动画框架函数

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);
        }
    }
}
for(砖中的变量i){
如果(!bricks[i].isDestroyed){
如果(
(this.y+this.radius==bricks[i].y-bricks[i].高度)
&&(this.x>=bricks[i].x)
&&(this.x=bricks[i].x)

&&(此.x冲突代码似乎在某些检查中使用==而您可能需要一个不等式。因此,我猜测球正在越过该确切位置,导致跳过其他检查。将所有比较切换为>=或修复代码


基本上,我将
==
改为
,这并没有描述球的运动方式,只是描述了球与砖的碰撞方式。没有足够的信息来回答你的问题。这就是我发布jsbin的原因。这并不复杂,我真的不能让它复杂化:D。基本上,当球到达x和y的某个坐标时(画布、球拍、希望砖块的末端)它改变其x和y“方向”,并开始移动,例如不是x=+1,而是x=-1
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);
            break;
        }
        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);
            break;
        }
        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);
            break;
        }
        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);
            break;
        }                  
    }
}