2

Is there a way to know if an AnimationTimer is running or not? I would like to toggle a pause menu like this:

setOnKeyTyped(event -> {
  switch (event.getCode()) {
    case SPACE:
      if (animationTimer.isRunning()) { // What should I put here?
        animationTimer.stop();
      }
      else {
        animationTimer.start();
      }
      break;

    default:
      break;
  }
}
SteeveDroz
  • 6,006
  • 6
  • 33
  • 65

1 Answers1

3

You can override start and stop to keep information about whether the timer is running or not:

class StatusTimer extends AnimationTimer {

    private volatile boolean running;

    @Override
    public void start() {
         super.start();
         running = true;
    }

    @Override
    public void stop() {
        super.stop();
        running = false;
    }

    public boolean isRunning() {
        return running;
    }

    ...

}

There seems to be no other way without accessing private members of AnimationTimer.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • The [`AnimationTimer`](https://docs.oracle.com/javafx/2/api/javafx/animation/AnimationTimer.html) doesn't have a `getStatus()` method. – SteeveDroz Nov 27 '16 at 07:08