0

I'm working on a very simple game in JavaFX. I have two objects for representing player:

  1. player - instance of Player class

  2. 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); } } 
3
  • All the code you've posted looks correct and should behave the way you want; so there are probably errors elsewhere in your code that prevent it working. Can you create a simple, complete example and edit your question to include it? Commented Jul 7, 2015 at 11:11
  • See also: How to write a KeyListener for JavaFX. Commented Jul 7, 2015 at 11:12
  • @James_D i extended the OP with full code (removed only parts that have nothing to do with this). Commented Jul 7, 2015 at 11:19

1 Answer 1

2

Your x and y properties in the Player class are incorrectly implemented.

When you call player.setY(...) you create a new DoubleProperty:

public void setY(double y) { this.y = new SimpleDoubleProperty(y); } 

and of course this is not the property to which your node was earlier bound. So it's value is not reflected in the node.

You need

public void setY(double y) { this.y.set(y); } 

and similarly for setX(...).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.