title: Moving Two Rectangles with keyboard in Javafx
body: <p>I'm fairly new to programming, and I've been working on a two player Pong game with JavaFX. One player uses W/S to move his "paddle" and the other player uses the Up/Down arrows for his. The problem I keep having is that only one player can move his paddle at a time. I can't figure out how to get it so they can each move their individual paddles at the same time. I had one keyboard event handler control both paddles, I thought that was the problem. I made two separate keyboard handlers, but got another set of problems tat I think is caused by the built in setFocusTraversable methods. I hope what I'm trying to do makes sense. Any ideas?</p>
  
  <pre><code>import javafx.event.EventHandler;
  import javafx.scene.Group;
  import javafx.scene.canvas.Canvas;
  import javafx.scene.canvas.GraphicsContext;
  import javafx.scene.input.KeyCode;
  import javafx.scene.input.KeyEvent;
  import javafx.scene.paint.Color;
  import javafx.scene.paint.Paint;
  import javafx.scene.shape.Rectangle;
  
  
  public class Game{
  
  Rectangle leftPaddle;
  double leftPaddleY = 260;
  
  Rectangle rightPaddle;
  double rightPaddleY = 260;
  
  public void createGame(Group gameDisplay){
  
      //creates background
      Rectangle background = new Rectangle(0,0,800,600);
      background.getStyleClass().add("background");
  
      //draws field lines
      Canvas game = new Canvas(800, 600);
      GraphicsContext gc = game.getGraphicsContext2D();
      gc.setStroke(Paint.valueOf("WHITE"));
      gc.setLineWidth(5);
      gc.strokeLine(400, 0, 400, 600);
      gc.strokeOval(300, 200, 200, 200);
      gc.strokeRect(0, 150, 100, 300);
      gc.strokeRect(700, 150, 100, 300);
      gc.setStroke(Paint.valueOf("BLACK"));
      gc.setLineWidth(8);
      gc.strokeRect(0, 0, 800, 600);
  
      //creates red paddle
      leftPaddle = new Rectangle(30, leftPaddleY, 20, 70);
      leftPaddle.setOnKeyPressed(paddleMovement);
      leftPaddle.setFocusTraversable(true);
      leftPaddle.setFill(Color.RED);
  
      //creates blue paddle
      rightPaddle = new Rectangle(750, rightPaddleY, 20, 70);
      rightPaddle.setOnKeyPressed(paddleMovement);
      rightPaddle.setFocusTraversable(true);
      rightPaddle.setFill(Color.BLUE);
  
  
      gameDisplay.getStylesheets().add(getClass().getResource("GameDisplay.css").toExternalForm());
      gameDisplay.getChildren().addAll(background, game, leftPaddle, rightPaddle);
  }
  
  public EventHandler&lt;KeyEvent&gt; paddleMovement = new EventHandler&lt;KeyEvent&gt;(){
  
      @Override
      public void handle(KeyEvent event) {
  
          //red paddle movement
          if(event.getCode().equals(KeyCode.W)){
              leftPaddle.setY(leftPaddleY -= 6);
              if(leftPaddle.getY() &lt; 0){
                  leftPaddle.setY(0);
                  leftPaddleY = 0;
              }
          }
          if(event.getCode().equals(KeyCode.S)){
              leftPaddle.setY(leftPaddleY += 6);
              if(leftPaddle.getY() &lt; 0){
                  leftPaddle.setY(0);
                  leftPaddleY = 0;
              }
          }
  
          //blue paddle movement
          if(event.getCode().equals(KeyCode.UP)){
              rightPaddle.setY(rightPaddleY -= 6);
              if(rightPaddle.getY() &lt; 0){
                  rightPaddle.setY(0);
                  rightPaddleY = 0;
              }
          }
          if(event.getCode().equals(KeyCode.DOWN)){
              rightPaddle.setY(rightPaddleY += 6);
              if(rightPaddle.getY() &lt; 0){
                  rightPaddle.setY(0);
                  rightPaddleY = 0;
              }
          }
  
      }
  };
  }
  </code></pre>