【发布时间】:2018-06-04 06:43:10
【问题描述】:
所以我有点坚持这个。我有一个非常基本的游戏,你可以用箭头键在网格周围移动一艘船。
我添加了另一个线程,其中包含一些应该自动在网格中漫游的怪物。我可以从打印语句中看到线程正在运行并且怪物正在移动,但是图像位置没有被更新。
我发现了一些类似的问题,并且有很多使用Platfrom.runLater 的建议。但我不确定它是否适合我的具体情况,如果适合,如何实施。
这就是 Monster 类正在做的事情,每秒将怪物向右移动一格。就像我提到的,每次调用 setX() 时我都会记录当前位置,因此我可以看到位置正在更新。
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.awt.Point;
public class Monster implements Runnable {
private Point currentPoint;
private OceanMap map;
public Monster(int x, int y) {
this.currentPoint = new Point(x, y);
this.map = OceanMap.getInstance();
}
public Point getLocation() {
System.out.println(this.currentPoint.toString());
return this.currentPoint;
}
private void setNewLocation(Point newLocation) {
this.currentPoint = newLocation;
}
private void setY(int newY) {
this.currentPoint.y = newY;
this.setNewLocation(new Point(this.currentPoint.x, this.currentPoint.y));
}
private void setX(int newX) {
this.currentPoint.x = newX;
this.setNewLocation(new Point(this.currentPoint.x, this.currentPoint.y));
System.out.println(this.currentPoint.toString());
}
// public void addToPane() {
// System.out.println("this is called");
// iv.setX(this.currentPoint.x + 1 * 50);
// iv.setY(this.currentPoint.y * 50);
// obsrvList.add(iv);
// }
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setX(this.currentPoint.x + 1);
}
}
}
这里是 JavaFX 线程。
/* Monster resources */
private Image monsterImage = new Image(getClass().getResource("monster.png").toExternalForm(), 50, 50, true, true);
private ImageView monsterImageView1 = new ImageView(monsterImage);
private Monster monster1;
private Thread monsterThread;
@Override
public void start(Stage oceanStage) throws Exception {
root = new AnchorPane();
scene = new Scene(root, scale * xDimensions, scale * yDimensions);
oceanStage.setScene(scene);
oceanStage.setTitle("Ocean Explorer");
/* Draw Grid */
for (int x = 0; x < xDimensions; x++) {
for (int y = 0; y < yDimensions; y++) {
Rectangle rect = new Rectangle(x * scale, y * scale, scale, scale);
rect.setStroke(Color.BLACK);
rect.setFill(Color.PALETURQUOISE);
root.getChildren().add(rect);
}
}
oceanStage.show();
monsterThread = new Thread(monster1);
monsterThread.start();
Platform.runLater(() -> {
monsterImageView1.setX(monster1.getLocation().x * scale);
monsterImageView1.setY(monster1.getLocation().y * scale);
root.getChildren().add(monsterImageView1);
});
startSailing();
}
如果需要,我可以提供更多代码,这是我目前认为相关的所有内容。
我的问题是,如何从另一个线程更新 JavaFX 线程的 UI?
【问题讨论】:
-
可以发一下
Monster的出处吗? -
不要将线程用于此类功能。使用
TimeLine -
您不需要其他线程。请改用 AnimationTimer。 NAimationTimer 中的 handle 方法为您提供以纳秒为单位的时间戳。经过 x 秒后,您使用
this.setX(this.currentPoint.x + 1); stop();的方法 -
@ChrisSmith 更新
-
虽然重复的答案是正确的方法,但我想我会解决你原来的问题。问题是您在
Monster类中更新currentPoint,但这个值永远不会传播到monsterImageView1。解决该特定问题的方法是将currentPoint转换为ObjectProperty<Point>,然后对其使用绑定。但我同意@James_D,TimeLine会是更好的方法。