[英]Recursive method acting weird
我正在制作一个打砖块游戏,由于一个未知的原因,我遇到了一个令人讨厌的bug,我无法追踪。 到目前为止游戏的状态:
这是我想要做的:假设我点击上图中圈出的红色按钮。 我希望红砖消失,而红色的红砖则适当地占据他们的位置。
到目前为止的代码:
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;
}
一些兴趣点:
编辑:谢谢大家! 您的意见和/或答案向我展示了继续前进的正确道路!
我没有足够的评论点,所以这更像是一个建议,但如果你在第一排,你不会想要删除砖吗? 这将与您在调试时发现存在问题的区域相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.