绑定属性不起作用

时间:2015-07-07 11:03:42

标签: java javafx

我正在使用JavaFX中的一个非常简单的游戏。我有两个代表玩家的对象:

  1. 播放器 - 播放器类的实例

  2. playerObj - Rectangle类的实例,用于在地图上表示玩家

  3. 现在我实现了玩家移动的基本功能,如goUp,goLeft等。这些都改变了玩家类中的属性X和Y(这些是DoubleProperty类型)。现在我想这样做,所以玩家的X和Y属性的每一个变化都会反映在playerObj中,所以我使用了这样的属性绑定:

     playerObj.yProperty().bind(player.yProperty());
    

    但是当我调用goUp()方法时,player对象中的“y属性”会改变,但是playerObj对象中的“y属性”不会改变。所以:

        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;
            }
        });
    

    会导致这种情况(在调用goUp()方法3次后):

    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
    

    当我将玩家Y属性绑定时,为什么playerObj Y属性也没有改变?

    编辑:完整代码(删除不相关的部分)

    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);
        }
    }
    

1 个答案:

答案 0 :(得分:2)

x类中的yPlayer属性未正确实施。

当您致电player.setY(...)时,您需要创建 DoubleProperty

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

当然,这不是您的节点之前绑定的属性。因此,它的值不会反映在节点中。

你需要

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

,同样适用于setX(...)