AnimationTimer stop()方法不会停止

时间:2017-10-10 23:02:16

标签: java animation

我知道可以使用handle()函数从其重写的this.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();

我做错了什么?

非常感谢任何帮助,

1 个答案:

答案 0 :(得分:0)

我把它弄清楚了。实际上是合乎逻辑的(通常是)。 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();
        }        
      }

谢谢大家的努力!