2014-05-21 2 views
-1

그래서 플레이어 클래스가있는 보드 게임을 만들려고합니다. 이 수업에서는 이사회에서의 지위, 돈의 금액 등과 같은 다른 것들을 저장하고 싶습니다.동일한 클래스의 별도 개체에 데이터 저장

문제는 두 명의 플레이어가 보드를 가로 질러 걸어가려고 할 때입니다. movePlayer 메서드에서 두 값을 더하고 10 칸 위로 이동합니다.

내가 여기에 뭔가 분명한 것을 놓치고 있습니까?

public class Board extends Component { 
private Player playerOne; 
private Player playerTwo; 
public Board(){ 
    playerOne = new Player(); 
    playerTwo = new Player(); 
} 

    g.fillOval(playerOne.getPositionX(), playerOne.getPositionY(), 50, 15); // draw player 1 

    g.fillOval(playerTwo.getPositionX(), playerTwo.getPositionY(), 15, 50); // draw player 2 

} 

public void rollDice(){ 
    playerOne.movePlayer(8); // move player1 > 8 spots 
    playerTwo.movePlayer(2); // move player2 > 2 spots    
} 
} 

movePlayer 방법 및 모든 그것과 관련된 :

public static void updatePlayerPos(int x){ 
    if (playerPos > 40){ 
     playerPos = 2; 
    } else { 
     playerPos = playerPos + x; 
    } 
} 

public static void changePosition(){ 
    if (playerPos < 12){ 
     positionX = positionX - 50; 
    }else if(playerPos < 22){ 
     positionY = positionY - 50; 
    } else if (playerPos < 32){ 
     positionX = positionX + 50; 
    }else if(playerPos < 42){ 
     positionY = positionY + 50; 
    } 
} 

public void movePlayer(int dicerolls){ 
    for (int i = 0;i < dicerolls;i++){ 
     updatePlayerPos(1); 
     changePosition(); 
    } 
} 
+0

movePlayer() 구현을 보여줄 수 있습니까? – Ksv3n

+0

Ksven이 정확합니다. Player.movePlayer가 필요합니다. 이 문제는 정적 변수를 사용하여 플레이어가 얼마나 멀리 이동했는지 파악할 수 있고 두 플레이어 중 하나에서 업데이트를 호출 할 수있는 것처럼 들릴 수 있습니다. –

+0

네 말이 맞아. 인스턴스 변수는 정적입니다./facepalm – MvdBeukel

답변

0

귀하의 ChangePosition 방법은 정적이며 일부 정적 변수를 수정합니다. 즉, 모든 플레이어가 좌표를 안전하게 지키지는 못하지만 모두 동일한 좌표이므로 두 가지 방법이 합쳐집니다. 해당 클래스의 모든 인스턴스에서 개별적인 필드를 만들려면 정적 수정자를 사용하지 않고 좌표를 선언해야합니다.