gpt4 book ai didi

JavaFX 更新问题?

转载 作者:行者123 更新时间:2023-11-30 06:13:04 27 4
gpt4 key购买 nike

我正在 JavaFX 中开发一个游戏,现在我正在尝试创建一个加载屏幕,因为加载资源需要一些时间。我创建了一个 LoadingPane 类来显示几个进度条,并且我确信它可以工作。但是,在下面的代码中,加载 Pane 直到 loadAssets 函数之后才可见,即使我事先添加了它。当我运行下面的代码时,我会看到一个空白阶段,显示资源加载所需的时间,然后出现一个带有已完成进度条的屏幕。

我还没有找到任何有类似问题的人,也没有找到任何类型的刷新或更新功能来强制场景在继续程序之前显示加载 Pane 。

注意:我删除了一些设置键盘输入处理的不相关代码。

public class Main extends Application{
Pane root = new Pane();
Scene mainScene = new Scene(root, Constants.WINDOW_WIDTH, Constants.WINDOW_HEIGHT, Color.BLACK);

static LoadingPane loadingPane = new LoadingPane(3);
private static int loadingIndex = 0;

public static void main(String[] args) {
if(Constants.DEBUG_MODE)
System.out.println("WARNING: Game has launched in debug mode!");
launch(args);
}

public static void updateProgress(double percent){
loadingPane.setBarLength(loadingIndex, percent);
}

public static void loadAssets(){
RoomLoader.createRooms();
updateProgress(1.0);
loadingIndex++;

ProjectileLoader.load("imgs/projectiles/");
ProjectileLoader.load(Constants.BATTLE_IMAGES_FILEPATH);
updateProgress(1.0);
loadingIndex++;

BattleLoader.createBattles();
updateProgress(1.0);
loadingIndex++;
}
public static void updateProgress(double percent){
loadingPane.setBarLength(loadingIndex, percent);
}

@Override
public void start(final Stage primaryStage) {

//root.getChildren().add(new javafx.scene.image.ImageView(new Image("imgs/loading.png")));
root.setLayoutX(0);

primaryStage.setScene(mainScene);
primaryStage.show();
primaryStage.toFront();

primaryStage.setTitle("Branch");
primaryStage.setResizable(false);
//primaryStage.getIcons().add(new Image("core/imgs/soul/red.png"));
//This allows the closing of the primaryStage to end the program
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>(){
@Override
public void handle(WindowEvent t) {
System.exit(0);
}
});

root.resize(Constants.WINDOW_WIDTH, Constants.WINDOW_HEIGHT);
primaryStage.getIcons().add(new Image("imgs/icon.png"));

//End GUI setup

//The problem lines
root.getChildren().add(loadingPane);
//refresh root?
loadAssets();
}
}

编辑:工作代码对于遇到类似问题的任何人,下面是我用来让它工作的代码:

我替换了这个:

//The problem lines
root.getChildren().add(loadingPane);
//refresh root?
loadAssets();

这样:

root.getChildren().add   (loadingPane);

Task<Integer> loadingTask = new Task<Integer>() {
@Override protected Integer call() throws Exception {
loadAssets();

return 1;
}
};

loadingTask.setOnSucceeded(new EventHandler<WorkerStateEvent>(){
@Override
public void handle(WorkerStateEvent t){
loadingPane.setVisible(false);
load(); //note: this function sets up the actual game
//updating the GUI, adding game elements, etc
}
});


new Thread(loadingTask).start();

我不能说这是最好的方法,但我可以说它有效。祝你好运!

最佳答案

您需要一个单独的线程来执行更新方法。

代码以线性方式运行,一位代码运行,然后运行下一位。通过单独的线程,两“行”代码可以并行运行。该进程运行并同时更新 GUI。

关于JavaFX 更新问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49826358/

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