-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();
}
}
movePlayer() 구현을 보여줄 수 있습니까? – Ksv3n
Ksven이 정확합니다. Player.movePlayer가 필요합니다. 이 문제는 정적 변수를 사용하여 플레이어가 얼마나 멀리 이동했는지 파악할 수 있고 두 플레이어 중 하나에서 업데이트를 호출 할 수있는 것처럼 들릴 수 있습니다. –
네 말이 맞아. 인스턴스 변수는 정적입니다./facepalm – MvdBeukel