0

I am trying to build an android game using java (this is the first time I have done this so I am very new). When the user selects they game they want to play from the main activity the GamePlay activity class is called. In this class I am trying to load a GameBoard view I have created and then run a loop that ends when the game is over. I have tried a few different methods of doing this (including threading) but the problem I am having is that the view does not load before the loop is entered. I end up with a black screen and an infinite loop. Any suggestions?

Note: the board has click detection that does successfully assign values to d1 and 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");
}

1 Answers1

0

Here is an image of an activity lifecycle to start with, in case you haven't seen it before. I find it very useful:

Activity Lifecycle

Can you post your gameboard code? The error might be in there.

Also try to add in your activity_game_play xml your GameBoard class like what was suggested in this answer: Add class to xml

Community
  • 1
  • 1
Ion
  • 334
  • 3
  • 15