title: Relocation of an image after a detection of a collision
body: <p>I have figured out how to detect the collision of the rectangle and the image but where I am having problems is when I call the method to relocate the image it does it like 10 times in a row then won't work again.</p>
  
  <p>I am making my first game in JavaFX. Im trying to make a basic snake game but haven't been able to figure out what is wrong with relocating the food after the snake collides with it.</p>
  
  <pre><code>public class Main extends Application {
  
      Stage window;
      Scene mainScene;
  
      private final int WIDTH = 500;
      private final int HEIGHT = 500;
  
      Timeline timeline;
  
      private Direction action = Direction.RIGHT;
  
      private Rectangle snakeHead;
      private final int speed = 3;
      private int xSpeed = 3;
      private int ySpeed = 3;
  
      private int snakeW = 20;
      private int snakeH = 20;
      private BoundingBox snakeBox;
  
      private BoundingBox foodBox;
  
      private ImageView food;
      private Random rand = new Random();
  
      private double foodX;
      private double foodY;
  
      enum Direction {
          LEFT,RIGHT,UP,DOWN;
      }
  
      private Parent createContent(){
          Pane root = new Pane();
  
          //food = new Food();
          food = new ImageView(new Image("resources/apple.png"));
          food.setFitHeight(25);
          food.setFitWidth(25);
          food.setPreserveRatio(true);
          newFood();
          foodBox = new BoundingBox(foodX,foodY,20,20);
  
  
          snakeHead = new Rectangle(snakeW,snakeH);
          snakeHead.setTranslateX(200);
          snakeHead.setTranslateY(200);
  
          snakeBox = new BoundingBox(snakeHead.getTranslateX(),snakeHead.getTranslateY(),snakeW,snakeH);
  
          timeline = new Timeline(new KeyFrame(Duration.millis(16), e-&gt; {
              //Snake movement
              if(action == Direction.LEFT) {snakeHead.setTranslateX(snakeHead.getTranslateX() - xSpeed);}
              if(action == Direction.RIGHT) {snakeHead.setTranslateX(snakeHead.getTranslateX() + xSpeed);}
              if(action == Direction.UP) {snakeHead.setTranslateY(snakeHead.getTranslateY() - ySpeed);}
              if(action == Direction.DOWN) {snakeHead.setTranslateY(snakeHead.getTranslateY() + ySpeed);}
  
              //Stops snake at edges of screen
              if(snakeHead.getTranslateX() &lt;= 0){
                  xSpeed = 0;
                  if(action == Direction.RIGHT){xSpeed = speed;}
              }
              if(snakeHead.getTranslateX() &gt;= WIDTH - snakeW){
                  xSpeed = 0;
                  if(action == Direction.LEFT){xSpeed = speed;}
              }
              if(snakeHead.getTranslateY() &lt;= 0){
                  ySpeed = 0;
                  if(action == Direction.DOWN){ySpeed = speed;}
              }
              if(snakeHead.getTranslateY() &gt;= HEIGHT - snakeH){
                  ySpeed = 0;
                  if(action == Direction.UP){ySpeed = speed;}
              }
  
              //TODO: Detect Collisions
                       if(foodBox.intersects(snakeHead.getTranslateX(),snakeHead.getTranslateY (),snakeW,snakeH)){
                  newFood();
                  System.out.println("Collision");
              }
  
          }));
  
  
  
          timeline.setCycleCount(Timeline.INDEFINITE);
  
          root.getChildren().addAll(snakeHead,food);
  
          return root;
      }
  
      private void newFood() {
          foodX = rand.nextInt(500);
          foodY = rand.nextInt(500);
  
          food.setTranslateX(foodX);
          food.setTranslateY(foodY);
  
          System.out.println("X " + foodX);
          System.out.println("Y " + foodY);
      }
  
      private void startGame() {
          timeline.play();
      }
  
      private void stopGame() {
          timeline.stop();
      }
  
  
      @Override
      public void start(Stage primaryStage) throws Exception{
          window = primaryStage;
          mainScene = new Scene(createContent(),WIDTH,HEIGHT);
  
          mainScene.setOnKeyPressed(e-&gt; {
              switch(e.getCode()) {
                  case UP: action = Direction.UP; break;
                  case DOWN: action = Direction.DOWN; break;
                  case LEFT: action = Direction.LEFT; break;
                  case RIGHT: action = Direction.RIGHT; break;
              }
          });
  
          window.setTitle("Snake");
          window.setResizable(false);
          window.setScene(mainScene);
          window.show();
  
          startGame();
      }
  
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>What I'm looking for is when the rectangle hits the apple it relocates. I have been struggling with this for awhile and don't know what to do. Im a fairly new programmer still.</p>