JavaFX:在窗格内同时移动数百个 ImageView(性能问题)
JavaFX: Moving hundreds of ImageViews simultaneously inside a Pane (Performance problems)
我目前正在为一个大学项目开发一款塔防游戏。
在更高的波浪中,有数百个敌人在四处移动。我的问题是它在大约 300 多个敌人同时移动时表现非常差。
每个敌人都是显示在我的场景中的窗格的 children。
我的 EnemyGraphics class 中有一个方法可以通过调用更新方法来更新位置:
public class EnemyGraphics extends ImageView implements EventObserver {
...
@Override
public void update() {
Platform.runLater(() -> {
relocate(enemy.getxCoordinate(), enemy.getyCoordinate());
});
}
}
我猜它变得迟钝了,因为每个敌人每次移动时都会通过调用 update() 自行更新其位置。
有没有一种方法可以在不重绘场景的情况下为我的 ImageView object 设置新坐标,并且在主要 FX-Thread 中创建一个定时器,在一定的时间间隔内重绘整个场景?或者我可以调用其他解决方案/方法在窗格上高效地移动图像吗?
好的,我发现了我的错误。每次我的 enemyLogic class 计算一个新位置时,它都会调用 enemyGraphic 对象来更新其位置。在测试期间,我删除了 enemyGraphics class 中方法的功能,但没有删除调用。
为了更新所有敌人的位置,我在我的 GUIcontroller 中写了一个方法 class:
private void startUpdateTicker() {
final AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long timestamp) {
updateEnemies();
}
};
timer.start();
}
public synchronized void updateEnemies() {
for (EnemieGUI enemy : enemyList) {
enemy.relocate(enemy.getEnemie().getxCoordinate(), enemy.getEnemie().getyCoordinate());
}
}
我目前正在为一个大学项目开发一款塔防游戏。 在更高的波浪中,有数百个敌人在四处移动。我的问题是它在大约 300 多个敌人同时移动时表现非常差。 每个敌人都是显示在我的场景中的窗格的 children。
我的 EnemyGraphics class 中有一个方法可以通过调用更新方法来更新位置:
public class EnemyGraphics extends ImageView implements EventObserver {
...
@Override
public void update() {
Platform.runLater(() -> {
relocate(enemy.getxCoordinate(), enemy.getyCoordinate());
});
}
}
我猜它变得迟钝了,因为每个敌人每次移动时都会通过调用 update() 自行更新其位置。
有没有一种方法可以在不重绘场景的情况下为我的 ImageView object 设置新坐标,并且在主要 FX-Thread 中创建一个定时器,在一定的时间间隔内重绘整个场景?或者我可以调用其他解决方案/方法在窗格上高效地移动图像吗?
好的,我发现了我的错误。每次我的 enemyLogic class 计算一个新位置时,它都会调用 enemyGraphic 对象来更新其位置。在测试期间,我删除了 enemyGraphics class 中方法的功能,但没有删除调用。
为了更新所有敌人的位置,我在我的 GUIcontroller 中写了一个方法 class:
private void startUpdateTicker() {
final AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long timestamp) {
updateEnemies();
}
};
timer.start();
}
public synchronized void updateEnemies() {
for (EnemieGUI enemy : enemyList) {
enemy.relocate(enemy.getEnemie().getxCoordinate(), enemy.getEnemie().getyCoordinate());
}
}