java - 递归方法行为怪异

标签 java swing recursion jpanel jbutton

我正在制作一款 BrickBreaker 游戏,但由于未知原因,我遇到了一个烦人的错误,我无法找到它。到目前为止的游戏状态:

BrickBreaker

这是我想要做的:假设我点击了上图中圈出的红色按钮。我希望红色的砖 block 消失,红色上方的砖 block 适本地占据它们的位置。

到目前为止的代码:

private void moveBrick(BrickHolder brickHolder) {

    Point brickHolderLocation = brickHolder.getBrickHolderLocation();

    Brick containedBrick = getBrickByXAndY(brickHolderLocation.x, brickHolderLocation.y); // getting the Brick at that location

    if (containedBrick == null) {
        // If in any case there should be no brick at that position, just go on with the Brick above
        if (brickHolderLocation.y == 0) { // Should we be at the top row, there's no need to continue
            return;
        } else {
            BrickHolder nextBrickHolder = getPanelByXAndY(brickHolderLocation.x, brickHolderLocation.y - 1);
            moveBrick(nextBrickHolder);
        }
    }

    if (brickHolderLocation.y == 0) { // Should we be at the top row, there's no need to continue
        return;
    }

    // Removing the current Contained Brick
    brickHolder.remove(containedBrick);

    // Getting the Brick I want to move, normally hosted at the above Panel
    Brick theOneToBeMoved = getBrickByXAndY(brickHolderLocation.x, brickHolderLocation.y - 1);

    if (theOneToBeMoved == null) {
        // If in any case the Panel above doesn't contain a Brick, then continue with the Panel above.
        BrickHolder nextBrickHolder = getPanelByXAndY(brickHolderLocation.x, brickHolderLocation.y - 1);
        moveBrick(nextBrickHolder);
    }

    // Getting the Panel above the current one, so that we may move the Brick hosted there,
    // To the current Panel
    BrickHolder toHoldTheNewBrick = getPanelByXAndY(brickHolderLocation.x, brickHolderLocation.y - 1);

    brickHolder.add(theOneToBeMoved); // Moving the Brick at the current Panel
    toHoldTheNewBrick.remove(theOneToBeMoved); // Removing that same brick from the Panel above
    theOneToBeMoved.setBrickLocation(brickHolderLocation); // Setting the Brick's new location.

    // Since we have gotten so far, we assume that everything worked perfectly and that it's time to continue
    // with the Panel above
    BrickHolder theNextOne = getPanelByXAndY(brickHolder.getBrickHolderLocation().x, brickHolder.getBrickHolderLocation().y - 1);

    moveBrick(theNextOne);
}

根据我所做的调试,我认为问题出在某处:

if (brickHolderLocation.y == 0) { // Should we be at the top row, there's no need to continue
            return;
        }

一些兴趣点:

  • Brick - 我创建的一个扩展 JButton 的类。而已。思考 将其作为带有背景的普通 JButton
  • BrickHolder - I 类 创建用于托管 Bricks。此类扩展了 JPanel。唯一的 addition 是一个 (Point) 位置变量,添加是为了更容易 操纵。

编辑:谢谢大家!您的评论和/或回答为我指明了继续前进的正确道路!

最佳答案

我没有足够的积分来评论,所以这更像是一个建议,但如果你在第一行,你不会还是想把砖移走吗?这与您在调试中确定存在问题的区域相同。

关于java - 递归方法行为怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11086635/

相关文章:

java - 为什么 setText 不更新 JLabel?

java - Swing中是否有监听启用/禁用事件的监听器?

python - 如何使用 beautifulsoup 递归查找网页中的所有链接?

java - 请求关注 JavaFX 阶段不会更改 macOS 左上角菜单栏标题

java - 区分要在 Struts 2 中使用的成功数据和错误消息?

java - 将饼图设置为 JLabel 而不是新框架

c++ - getNth() 堆栈中的项目

javascript - 递归 setTimeout 使堆栈增长

java - 从字符串中返回两个子字符串

java - 私有(private)字符串属性在反编译的 .java 类 Kotlin 中可以为空