자바를 사용하여 Android 게임을 만들려고합니다. (처음으로이 작업을 수행했기 때문에 새로운 게임입니다.) 사용자가 메인 액티비티에서 플레이하고 싶은 게임을 선택하면 GamePlay 액티비티 클래스가 호출된다. 이 클래스에서는 내가 만든 GameBoard 뷰를로드하려고 시도하고 게임이 끝나면 끝나는 루프를 실행합니다. 이 (스레딩을 포함하여) 몇 가지 다른 방법을 시도했지만 내가 가지고있는 문제는 루프가 입력되기 전에보기가로드되지 않는다는 것입니다. 나는 검은 화면과 무한 루프로 끝난다. 어떤 제안?Android 액티비티가 뷰를 시작하고 onStart를 시작합니다.
참고 : 보드에는 d1 및 d2에 값을 성공적으로 할당하는 클릭 감지 기능이 있습니다.
public class GamePlay extends Activity{
private Player P1;
private Player P2;
private GameBoard board;
private Player turn;
private boolean gameOver = false;
private Thread thread;
//Variables associated with turn
private Dot d1 = null;
private Dot d2 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_play);
Bundle extra = getIntent().getExtras();
String type = extra.getString("GAME_TYPE");
//Set up appropriate players
if (type.equals(StartDB.ONE_PLAYER)){
this.setTitle("One Player Dots And Boxes");
String P1Name = "Person";
P1 = new HumanPlayer(Color.RED, P1Name);
P2 = new AIPlayer(Color.BLUE, "Computer");
} else {
this.setTitle("Two Player Dots And Boxes");
String P1Name = "Player 1";
String P2Name = "Player 2";
P1 = new HumanPlayer(Color.RED, P1Name);
P2 = new HumanPlayer(Color.BLUE, P2Name);
}
//Initialize turn to first player
setTurn(P1);
//Set up a game board
board = new GameBoard(this, this);
setContentView(board);
}
@Override
protected void onStart() {
super.onStart();
//Create turn thread and run it
while (!gameOver){
while(d2 == null){
try {
Thread.sleep(1000);
} catch (InterruptedException e){
e.printStackTrace();
}
}
System.out.println("Making a line");
Line line = new Line(d1, d2);
board.addLine(line);
d1 = null;
d2 = null;
board.invalidate();
//This was another attempt made:
//try {
//thread = new Thread(new GameLoop(this));
//thread.start();
//thread.join();
//} catch (InterruptedException e) {
//e.printStackTrace();
//}
checkGameOver();
}
System.out.println("Game Over");
}