我如何知道AnimationTimer是否正在运行? [英] How can I know if an AnimationTimer is running?

查看:180
本文介绍了我如何知道AnimationTimer是否正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法知道 AnimationTimer 是否正在运行?我想像这样切换一个暂停菜单:

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;
  }
}


推荐答案

您可以覆盖 start 停止以保存有关计时器是否正在运行的信息:

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;
    }

    ...

}

似乎没有其他方法可以访问 AnimationTimer 的私有成员。

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

这篇关于我如何知道AnimationTimer是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆