android中的砖破坏者游戏 - 检测命中(破球)

时间:2013-12-23 23:09:03

标签: java android collision-detection collision

我的代码现在是

private boolean checkHit2(int row, int col, Ball ball) {
    // Check for a hit from below
    if (ball.getTop()   <= this.getBrickBottom( row) &&
            ball.getTop()  >= this.getBrickTop(row) &&
            ball.getRight() >= this.getBrickLeft(col) &&
            ball.getLeft()  <= this.getBrickRight(col))
        return true;

    // Check for a hit from up
    if(ball.getBottom()  >= this.getBrickTop(row) &&
            ball.getBottom()  <= this.getBrickBottom(row) &&
            ball.getRight() >= this.getBrickLeft(col) &&
            ball.getLeft()  <= this.getBrickRight(col))
        return true;


    // No hit
    return false;
}

if(gameLevel.updateIfHit2(ballArr[i])){
                        if(ballArr[i].isBounceUp())
                            ballArr[i].down();
                        else
                            ballArr[i].bounceUp();
}

大多数时候哪个工作做得好。问题是,有时它会在同一块砖上检测到多个点击,因为代码“checkHit2”中的条件仍然成立(true)。 (每块砖需要超过1次才能消失,有时在击中砖之后它继续击中而不是改变方向(例如,而不是向下)。

我也试过这段代码:

private boolean checkHit(int row, int col, Ball ball) {
        // Check for a hit from below
        if (ball.getTop()   == this.getBrickBottom( row) &&
        //      ball.getTop()  >= this.getBrickTop(row) &&
                ball.getRight() >= this.getBrickLeft(col) &&
                ball.getLeft()  <= this.getBrickRight(col))
            return true;

        // Check for a hit from up
        if(ball.getBottom()  == this.getBrickTop(row) &&
            //  ball.getBottom()  <= this.getBrickBottom(row) &&
                ball.getRight() >= this.getBrickLeft(col) &&
                ball.getLeft()  <= this.getBrickRight(col))
            return true;


        // No hit
        return false;
    }

但它也有问题

2 个答案:

答案 0 :(得分:0)

您应该在checkhit2方法中使用synchronized,以便在与其他处理交错之前完成每个处理。

答案 1 :(得分:0)

如果你的球比你的球小,并且你的更新率不够好,那么球可能会在你的砖内直接移动,这将给你描述的结果。如果是这种情况,您可以:

  • 提高您的更新率(确定球之前的距离 打击测试较小)这对于拥有的手机来说仍然是一个问题 表现不佳。
  • 进行第三次测试,如果球完全在砖内 (两种情况都是真的)那么你需要根据反弹决定 关闭以前的状态。

你的游戏一切顺利!

相关问题