简体   繁体   English

弹跳球制动砖游戏,未能反弹砖

[英]Bouncing ball braking bricks game, fails to bounce of a brick

So my problem is that the ball just passes trough the bricks, nothing happens. 所以我的问题是球刚穿过砖块,没有任何反应。 I will put the whole code in jsbin, here I post only the piece that is not working. 我将整个代码放在jsbin中,这里我只发布不起作用的那个。 As you can see, I got a bouncing ball, a racket, bricks and an animationFrame function. 如你所见,我得到了一个弹跳球,一个球拍,一块砖和一个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 code: http://jsbin.com/eZOxusaQ/1/watch?html,js,output Jsbin代码: http ://jsbin.com/eZOxusaQ/1/watch?html,js,output

It looks like the collision code is using == on some of the checks when you probably want an inequality. 当您可能需要不等式时,看起来碰撞代码在某些检查中使用==。 So my guess is the ball is stepping over that exact position, resulting in the other checks being skipped. 所以我的猜测是球越过那个确切的位置,导致其他检查被跳过。 Switch all comparisons to be >= or <= as appropriate and see if that fixes it. 根据需要将所有比较切换为> =或<=并查看是否修复了它。

Fixed your code. 修复了你的代码。

Basically, I changed the == to <= , as the ball will never hit this EXACT spot. 基本上,我将==更改为<= ,因为球永远不会打到这个精确的位置。 Furthermore I added a break inside each of the if -conditions to quit the check when one brick got hit, as you want the ball to jump off and not to exterminate a whole row. 此外,我在每个if添加了一个break ,当一块砖被击中时退出检查,因为你希望球跳下来而不是消灭整排。

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;
        }                  
    }
}

EDIT 编辑
You will get a problem when your ball hits an "inner brick", for example a brick one row above a row with still bricks left. 当你的球撞到“内砖”时,你遇到问题,比如在一排上面还有一排砖,还剩下砖块。 As your ball will exterminate these bricks with your current conditions, as the coordinates of the ball will be smaller than the coordinates of your brick. 因为你的球将根据你当前的条件消灭这些砖块,因为球的坐标将小于砖块的坐标。 But that's more a general problem of your logic. 但这更像是你逻辑的一般问题。 You'll need to include more checks for this one. 您需要为此包含更多检查。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM