mercredi 22 novembre 2017

Why if block doesn't work

boolean gameContinues = true;
gameWindow.addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() >= KeyEvent.VK_LEFT && e.getKeyCode() <= KeyEvent.VK_DOWN) {
            directionGoing = e.getKeyCode() - KeyEvent.VK_LEFT;
            gameStart = true;
        }
    }
});

while (gameContinues) {
//  for(int i = 0; i < 1000000000; i++) {
//      for(int j = 0; j < 1000000000; j++) {
//          
//      }   
//  }
    if (gameStart) {
        start();
        gameContinues = false;              
    }
}

private void start() {
    // game operations
    System.out.println("started");
}

gameStart is a global variable, and initial value is false. Normally, when I hit left, right, up or down buttons, gameStart variable sets true, but if(gameStart) doesn't work. But when I slow down with the commented for blocks, if(gameStart) runs properly. Why this happens?

Aucun commentaire:

Enregistrer un commentaire