2012-06-18 4 views
1

BrickBreaker 게임을 만들고 있는데, 알 수없는 이유로 괴롭히는 버그로 인해 추적 할 수 없습니다. 지금까지 게임의 상태 : 빨간색 버튼은 위의 이미지에 동그라미에 내가 클릭 가정 : 여기이상한 행동 재귀 메서드

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 - 클래스 I 은 Bricks를 호스팅하기 위해 만들어졌습니다. 이 클래스는 JPanel을 확장합니다. 추가는 조작을 쉽게하기 위해 추가 된 (포인트) 위치 변수입니다.

편집 : 모두에게 감사드립니다! 귀하의 의견 및/또는 답변은 나에게 올바른 길을 보여주었습니다!

+0

[CodeReview] (http://codereview.stackexchange.com/)로 옮길 수도 있습니다 ... – SimplyPanda

+0

저는 아직 코드 논리를 이해하지 못합니다. 두 번이나 패스합니까? 하나는 같은 색의 인접한 벽돌을 제거하고 두 번째 단계는 벽돌을 다시 배포하는 것입니다. –

+0

정확한 JPanel에 포함 된 버튼을 제거한 다음 위의 Button을 가져 와서 이전 Jpanel로 이동하고 맨 위 행에 도달 할 때까지 계속 유지하는 것이 가장 좋은 방법입니다. – NlightNFotis

답변

4

코멘트가 충분하지 않아 제안 사항이 더 많습니다.하지만 맨 위 행에 있다면 상관없이 벽돌을 제거하고 싶지 않으십니까? 그건 당신이 디버깅에서 문제가되는 것과 동일한 영역에있을 것입니다.

+0

그래서 JPanel의 위치를 ​​이동 한 후에 확인해 보시기 바랍니다. – NlightNFotis

+0

당신이 사용해야한다고 생각합니다 brickHolder.remove (containedBrick); 전에 if (brickHolderLocation.y == 0) { return; } – Addison