title: Increase JavaFX KeyFrame speed without cyclic dependency?
body: <p>I'm trying my hand at creating a Snake clone with JavaFX. One of the requirements for the game is to have a sort of powerup food, which increases the speed of the snake.</p>
  <p>For my game loop, I'm using a <code>Timeline</code> with a single <code>KeyFrame</code>, which acts as the game tick. Every game object is rendered onto a <code>Canvas</code>.</p>
  <p>The snake moves in a 21x21 grid with each cell being 40px in length. In my implementation, the snake moves one cell each tick (by design). Each body segment is essentially moving 40px in the current direction. This means that the velocity of the snake is fixed by the game's tick duration.</p>
  <p>To increase the velocity of the snake, I have to change the duration of the keyframe, which updates game state and draws to the canvas. To do so I stop the timeline, remove the keyframe, and add a new keyframe with a shorter duration.</p>
  <p>The problem is that in order to do this, I have to call a method within the <code>Game</code> class, which contains the game loop, from one of it's dependencies (such as the <code>State</code> class).</p>
  <pre><code>private final Timeline loop = new Timeline();
  </code></pre>
  <pre><code>    public void setGameSpeed(GameSpeed speed) {
          createLoop(speed.value);
      }
  </code></pre>
  <pre><code>    private void createLoop(double duration) {
          if (loop.getStatus().equals(RUNNING)) {
              loop.stop();
          }
  
          if (loop.getKeyFrames().size() &gt; 0) {
              loop.getKeyFrames().removeAll(loop.getKeyFrames());
          }
  
          loop.getKeyFrames().add(new KeyFrame(Duration.seconds(duration), e -&gt; {
              state.update();
              renderer.draw();
          }));
          loop.play();
      }
  </code></pre>
  <p>I've come up with two solutions so far. I could either have the Timeline and associated methods be static, or pass the instance of &quot;Game&quot; to the respective dependency.</p>
  <p>Is there a more elegant way to do this?</p>