私はBrickBreakerゲームを作成していますが、不明な理由で、追跡できない厄介なバグで立ち往生しています。これまでのゲームの状態:
これが私がやりたいことです:上の画像で丸で囲んだ赤いボタンをクリックするとします。赤レンガを消し、赤の上にあるレンガを適切な位置に配置してほしい。
これまでのコード:
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;
}
いくつかの興味深い点:
- ブリック-JButtonを拡張するために作成したクラス。これ以上何もない。BackGroundを備えたプレーンなJButtonと考えてください
- BrickHolder-Bricksをホストするために作成したクラス。このクラスはJPanelを拡張します。唯一の追加は、操作を容易にするために追加された(ポイント)位置変数です。
編集:みんなありがとう!あなたのコメントや回答は私に続けるための正しい道を示しました!