I'm working on a very simple game in JavaFX. I have two objects for representing player:
player - instance of Player class
playerObj - instance of Rectangle class, used for representating player on the map
Now I implemented basic functions for player movement like goUp, goLeft etc. These all change properties X and Y in player class (these are DoubleProperty type). Now I want to make it so every change of player's X and Y property will reflect in playerObj so I used property binding like this:
playerObj.yProperty().bind(player.yProperty()); But when I invoke the goUp() method, the "y property" in player object will change, but "y property" in playerObj object won't change. So:
pane.setOnKeyPressed(e -> { switch(e.getCode()) { case UP: player.goUp(); System.out.println("player Y property: "+player.yProperty().getValue()); System.out.println("playerObj Y property: "+playerObj.yProperty().getValue()); break; } }); Will result in this (after invoking goUp() method 3 times):
player Y property: 245.0 playerObj Y property: 250.0 player Y property: 240.0 playerObj Y property: 250.0 player Y property: 235.0 playerObj Y property: 250.0 Why didn't the playerObj Y property change too when I binded it with player Y property?
EDIT: Full code (removed irrelevant parts)
Player.java
public class Player { private DoubleProperty x; private DoubleProperty y; private Scene scene; public Player(Scene scene) { this(DEFAULT_NAME, DEFAULT_COLOR, scene); } public Player(String name, Color color, Scene scene) { this.name = new SimpleStringProperty(name); this.color = color; this.scene = scene; } public Rectangle drawPlayer() { Rectangle player = new Rectangle(getX(),getY(),SIZE_OF_PLAYER,SIZE_OF_PLAYER); player.setFill(color); return player; } public Player goUp() { if(getY() != 0 && getY() != scene.getHeight()) { setY(yProperty().subtract(5).getValue()); } } public DoubleProperty xProperty() { return x; } public DoubleProperty yProperty() { return y; } public double getX() { return x.getValue(); } public void setX(double x) { this.x = new SimpleDoubleProperty(x); } public double getY() { return y.getValue(); } public void setY(double y) { this.y = new SimpleDoubleProperty(y); } } Game.java
public class Game extends Application { private Scene scene; private Player player; private Rectangle playerObj; private Pane pane; @Override public void start(Stage primaryStage) { pane = new Pane(); scene = new Scene(pane,500,500); player = new Player(scene); player.setX(scene.getWidth() / 2); player.setY(scene.getHeight() / 2); playerObj = player.drawPlayer(); pane.getChildren().add(playerObj); playerObj.yProperty().bind(player.yProperty()); pane.setOnKeyPressed(e -> { switch(e.getCode()) { case LEFT: player.goLeft(); break; case UP: player.goUp(); System.out.println("player Y property: "+player.yProperty().getValue()); System.out.println("playerObj Y property: "+playerObj.yProperty().getValue()); break; case RIGHT: player.goRight(); break; case DOWN: player.goDown(); } }); pane.requestFocus(); primaryStage.setScene(scene); primaryStage.setTitle("Game"); primaryStage.setResizable(false); primaryStage.show(); } public static void main(String[] args) { Application.launch(args); } }