【问题标题】:brick breaker game in android - detect hit(ball with break)android中的破砖游戏 - 检测命中(带休息的球)
【发布时间】:2013-12-24 09:07:13
【问题描述】:

我现在的代码是

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”中的条件仍然成立(真)。 (每块砖需要超过 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;
    }

但这也有问题

【问题讨论】:

    标签: java android collision-detection collision


    【解决方案1】:

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

    【讨论】:

    • 这不是问题-我只有一个线程调用此方法。问题是在线程的循环中它调用方法可能很频繁..
    【解决方案2】:

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

    • 提高更新率(使球在 命中测试更小),这对于拥有 性能低下。
    • 进行第三次测试,如果球完全在砖内 (这两种情况都是正确的)那么您需要根据 关闭球的先前状态。

    祝你的游戏一切顺利!

    【讨论】:

    • 谢谢。第一个建议对我来说并不好。关于第二个 - 你写了“如果球完全在砖里面” - 我描述的问题也可能是如果只有一个条件成立
    • 如果不了解有关何时/如何调用 checkHit 方法的更多信息,我无法为您提供太多帮助。尽管给出了您的评论,但也许一个简单的解决方案是在您检测到击球后立即移动球?或者,存储该帧期间球击中的行/列 ID 列表,并检查当前行/列是否未经过测试? (我不推荐这种方法)。正如我所说,我没有看到您的代码有什么特别的问题,所以您的问题可能出在其他地方。祝你好运! :)
    猜你喜欢
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多