Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Build a JavaFX Application It's About Time Completion Events

Jaden Kuhn
Jaden Kuhn
928 Points

Attempting to create very simple game need MAJOR help

I've been trying this very simple game for 2 days now. Ive ran into how to make a keyevent problem, got that fixed. Now i want it so when a certain key is pressed it moves an image element on the screen. I cannot figure it out and im getter very frustrated.

Here is my fxml <?import javafx.scene.control.Button?> <?import javafx.scene.effect.DropShadow?> <?import javafx.scene.effect.Glow?> <?import javafx.scene.image.Image?> <?import javafx.scene.image.ImageView?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.shape.Rectangle?>

      <AnchorPane  maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" 
    prefHeight="800.0" prefWidth="800.0" fx:controller="sample.Controller" xmlns="http://javafx.com/javafx/8.0.121" 
    <ImageView fitHeight="800.0" fitWidth="800.0" pickOnBounds="true" preserveRatio="true">
            <Image url="/images/background.png" />
      <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#222222" height="40.0" layoutY="380.0" stroke="BLACK" 
     strokeType="INSIDE" width="800.0">
            <Glow level="1.0">
                    <DropShadow />
        <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#222222" height="800.0" stroke="BLACK" strokeType="INSIDE" 
   width="20.0" />
        <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#222222" height="800.0" layoutX="786.0" stroke="BLACK" 
   strokeType="INSIDE" width="20.0" />
        <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#222222" height="20.0" layoutX="20.0" stroke="BLACK" 
  strokeType="INSIDE" width="767.0" />
        <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#222222" height="20.0" layoutX="20.0" layoutY="780.0" 
  stroke="BLACK" strokeType="INSIDE" width="767.0" />

     <ImageView fitHeight="150.0" fitWidth="200.0" layoutX="320.0" layoutY="20.0" pickOnBounds="true" 
    preserveRatio="true" fx:id="whiteShipID">
            <Image url="/style/whiteShip2.png" />
        <ImageView fitHeight="150.0" fitWidth="200.0" layoutX="320.0" layoutY="630.0" pickOnBounds="true" 
    preserveRatio="true" fx:id="blackShipID">
            <Image url="/style/blackShip2.png" />


Here is my Controller start event

    package sample;

   import javafx.animation.AnimationTimer;
   import javafx.event.ActionEvent;
   import javafx.event.EventHandler;
 import javafx.fxml.FXML;
            import javafx.fxml.FXMLLoader;
   import javafx.scene.Group;
    import javafx.scene.Parent;
     import javafx.scene.Scene;
     import javafx.scene.control.Button;
   import javafx.scene.image.Image;
   import javafx.scene.image.ImageView;
   import javafx.scene.input.KeyCode;
   import javafx.scene.input.KeyEvent;
   import javafx.stage.Stage;

   import java.util.ArrayList;

    public class Controller{
        Main mainOb = new Main();
        boolean right = false;
        double whiteX = 320;
        double blackX = 320;

public ImageView whiteShipID;

public void playGame(ActionEvent event) throws Exception{
    Parent d = FXMLLoader.load(getClass().getResource("../style/play.fxml"));

    Stage playStage = new Stage();

    Scene scene = new Scene(d);
    playStage.setTitle("Titan Clash");

    if(whiteShipID == null) {

    ArrayList<String> input = new ArrayList<String>();

            new EventHandler<KeyEvent>() {
                public void handle(KeyEvent e) {
                    String code = e.getCode().toString();

                    // only add once... prevent duplicates
                    if (!input.contains(code))
                    if(input.contains("RIGHT")) {
                        right = true;


            new EventHandler<KeyEvent>() {
                public void handle(KeyEvent e) {
                    String code = e.getCode().toString();
                    right = false;
    AnimationTimer animate = new AnimationTimer() {

        public void handle(long arg0) {
            if(right) {