2017-10-10 92 views
1

我知道可以使用this.stop函數從正在重寫的handle()方法中停止正在進行的動畫。下面的代碼來自遊戲,在遊戲結束並顯示警報後,它應該停止運行動畫。相反,警報彈出無數次。AnimationTimer stop()方法不會停止

new AnimationTimer() { 
    @Override 
    public void handle(long now) { 
    if (game.isInProgress()) { 
     nextFrame(); 
    } else { 
     if (game.isWon()) { 
     Alert alert = new Alert(AlertType.INFORMATION); 
     alert.setTitle("Game Over!"); 
     alert.setHeaderText("Congratulations, you have won the game!"); 
     alert.setContentText(toolbarModule + ": " + game.getLevel().getScoreObject().getScore() 
      + ", Time Remaining: " + toolbarModule.getTimeString() + "\n Rank: " 
      + toolbarModule.getRankString()); 
     alert.show(); 
     } else { 
     Alert alert = new Alert(AlertType.INFORMATION); 
     alert.setTitle("Game Over!"); 
     alert.setHeaderText("Sadly, you have lost the game!"); 
     alert.setContentText("Time passed: " + game.getLevel().getTimeLimit()); 
     alert.show(); 
     } 
     new EndScene(game, stage); 
     this.stop(); 
    } 
    } 
}.start(); 

我在做什麼錯?

任何幫助,將大大讚賞,

回答

0

我figuered出來。其實是合乎邏輯的(通常是)。 alert.show函數等待用戶確認彈出窗口。與此同時,handle()方法多次運行該方法。我需要的是運行一次這個彈出窗口。我通過包含警報和條件來做到這一點。像這樣:

 if (!victoryMessageReceived) { 
     victoryMessageReceived = true; 
     if (game.isWon()) { 
      Alert alert = new Alert(AlertType.INFORMATION); 
      alert.setTitle("Game Over!"); 
      alert.setHeaderText("Congratulations, you have won the game!"); 
      alert.setContentText(toolbarModule + ": " 
       + game.getLevel().getScoreObject().getScore() + ", Time Remaining: " 
       + toolbarModule.getTimeString() + "\n Rank: " + toolbarModule.getRankString()); 
      alert.show(); 
     } else { 
      Alert alert = new Alert(AlertType.INFORMATION); 
      alert.setTitle("Game Over!"); 
      alert.setHeaderText("Sadly, you have lost the game!"); 
      alert.setContentText("Time passed: " + game.getLevel().getTimeLimit()); 
      alert.show(); 
     }   
     } 

謝謝大家的努力!