So I'm trying to write code for a game that will let me start the game over when the player loses. I use a while loop (marked "zeroth loop") to enable the game to begin again. I use a timer which will stop itself when the game is over. Then, to keep the zeroth loop from running rampant, I have an empty inner loop (marked "first loop") which runs until the game is over. Once the game is over, I use another inner loop (marked "second loop") to "listen" for an arrow key press (which is not the focus of this question).
Here is my question. For some reason, the second loop isn't running only when I do not have a print statement in the first loop. If I put any print statement in the first loop, the second loop will run when the game ends. Leaving the first loop empty seems to keep from exiting, and as a result it keeps the code from progressing. Why is this happening? (NOTE: I tried putting something other than a print statement in the first loop and the second loop did not run)
Here is an outline of the important code:
// zeroth loop
while (true) {
//...
Timer timer = new Timer(speed, null);
timer.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//...
if (endGame) {
timer.stop();
}
}
});
timer.start();
// first loop
while (!endGame) { /* empty loop */ }
boolean arrowKeyNotPressed = true;
// second loop
while (arrowKeyNotPressed) {
System.out.println("second loop");
//...
// arrowKeyNotPressed = false;
}
}