title: How do I implement game-loop using timeline in Javafx?
body: <p>I am a beginner. I am making a simple color matching 2D game using Javafx where a circle will pass through obstacles of rectangles.<br/>
  The circle must pass through the rectangle which has the same color. <br/>
  After passing, the color of the circle and the sequence of colors of rectangles will change. <br/>
  I have done the following code. Now I don't have any clue how can I change sequence of colors of the rectangle obstacles and the color of the circle for each cycle of timeline. <br/>
  Also how can I detect if the color of the circle and obstacle has matched or not... need help quickly</p>
  
  <pre><code>import javafx.animation.AnimationTimer;
  import javafx.animation.KeyFrame;
  import javafx.animation.KeyValue;
  import javafx.animation.Timeline;
  import javafx.animation.TranslateTransition;
  import javafx.application.Application;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.scene.Group;
  import javafx.scene.Scene;
  import javafx.scene.*;
  import javafx.scene.control.Label;
  import javafx.scene.input.*;
  import javafx.scene.control.Button;
  import javafx.scene.layout.Pane;
  import javafx.scene.layout.StackPane;
  import javafx.scene.paint.Color;
  import javafx.scene.shape.Circle;
  import javafx.scene.shape.Rectangle;
  import javafx.stage.Stage;
  import javafx.util.Duration;
  
  public class ColorRun extends Application {
  
      private static final int KEYBOARD_MOVEMENT_DELTA = 20;
      private static final Duration TRANSLATE_DURATION = Duration.seconds(0.25);
  
      @Override
      public void start(Stage primaryStage) {
  
          for (int i = 0; i &lt; 12; i++) {
          Circle circle = new Circle();
          circle.setCenterX(650);
          circle.setCenterY(500);
          circle.setRadius(40);
          circle.setFill(Color.RED);
  
          Rectangle rectangle1 = new Rectangle(0, 0, 200, 70);
          rectangle1.setFill(Color.RED);
          rectangle1.setStroke(Color.BLACK);
          rectangle1.setArcWidth(10);
          rectangle1.setArcHeight(10);
  
          Rectangle rectangle2 = new Rectangle(200, 0, 200, 70);
          rectangle2.setFill(Color.GREEN);
          rectangle2.setStroke(Color.BLACK);
          rectangle2.setArcWidth(10);
          rectangle2.setArcHeight(10);
  
          Rectangle rectangle3 = new Rectangle(400, 0, 200, 70);
          rectangle3.setFill(Color.BLUE);
          rectangle3.setStroke(Color.BLACK);
          rectangle3.setArcWidth(10);
          rectangle3.setArcHeight(10);
  
          Rectangle rectangle4 = new Rectangle(600, 0, 200, 70);
          rectangle4.setFill(Color.YELLOW);
          rectangle4.setStroke(Color.BLACK);
          rectangle4.setArcWidth(10);
          rectangle4.setArcHeight(10);
  
          Pane root = new Pane();
          root.getChildren().addAll(circle, rectangle1, rectangle2, rectangle3, rectangle4);
          final Scene scene = new Scene(root, 800, 800, Color.GREY);
          primaryStage.setTitle("Color Run");
          primaryStage.setScene(scene);
          moveCircleOnKeyPress(scene, circle);
  
              Timeline timeline1 = new Timeline();
              timeline1.setCycleCount(Timeline.INDEFINITE);
              timeline1.setAutoReverse(false);
              final KeyValue kv1 = new KeyValue(rectangle1.yProperty(), 800);
              final KeyValue kv2 = new KeyValue(rectangle2.yProperty(), 800);
              final KeyValue kv3 = new KeyValue(rectangle3.yProperty(), 800);
              final KeyValue kv4 = new KeyValue(rectangle4.yProperty(), 800);
              final KeyFrame kf = new KeyFrame(Duration.millis(2000), kv1, kv2, kv3, kv4);
              timeline1.getKeyFrames().add(kf);
              timeline1.play();
  
  
          primaryStage.show();
          }
      }
  
      public void moveCircleOnKeyPress(Scene scene, Circle circle) {
          scene.setOnKeyPressed(new EventHandler&lt;KeyEvent&gt;() {
              @Override
              public void handle(KeyEvent event) {
                  switch (event.getCode()) {
  
                      case RIGHT:
                          circle.setCenterX(circle.getCenterX() + KEYBOARD_MOVEMENT_DELTA);
                          break;
                      case LEFT:
                          circle.setCenterX(circle.getCenterX() - KEYBOARD_MOVEMENT_DELTA);
                          break;
                  }
              }
          });
      }
  
      public static void main(String[] args) {
          launch(args);
  
      }
  }
  </code></pre>