gpt4 book ai didi

java - 绑定(bind)属性不起作用

转载 作者:行者123 更新时间:2023-12-02 04:32:23 24 4
gpt4 key购买 nike

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

  1. player - Player 类的实例

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

现在我实现了玩家移动的基本功能,如 goUp、goLeft 等。这些都更改了玩家类中的属性 X 和 Y(这些是 DoubleProperty 类型)。现在我想让玩家的 X 和 Y 属性的每次更改都会反射(reflect)在playerObj 中,因此我使用了如下属性绑定(bind):

 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

为什么当我将playerObj Y属性与player Y属性绑定(bind)时,它也没有改变?

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

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

游戏.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);
}
}

最佳答案

Player 类中的 xy 属性未正确实现。

当你调用player.setY(...)时,你创建了一个DoubleProperty:

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

当然,这不是您的节点之前绑定(bind)的属性。所以它的值(value)并没有体现在节点上。

你需要

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

对于 setX(...) 也类似。

关于java - 绑定(bind)属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31266805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com