I am currently working with JavaFX on a game program. What I have planned here is a loading panel that in ideal sequence:
- switches the main stage to the loading scene
- waits for 1s
- sets visible a panel that shows the loading status
- waits for another 1s
- creates a world
5.a) assigns a random seed to the world
5.b) waits for 1s
5.c) sets coordinates
5.d) waits for 1s
5.e) assigns essential arrays
Instead, this is what happens:
waits for 4~5s
does steps #1,3,5a,5c,5e.
How do I ensure that the program follows the ideal sequence? I have tried a new thread task and it worked, but the world constructor cannot comply with the thread and cause an error.
private static void loading() {
Main.loading = new Scene(new Loading(), 1280, 720);
Main.stage.setScene(Main.loading);
try {
Thread.sleep(1000);
((Loading) Main.loading.getRoot()).status.setVisible(true);
Thread.sleep(1000);
Main.currentWorld = new World();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
WORLD CONSTURCTOR
seed = ThreadLocalRandom.current().nextInt();
((Loading) Main.loading.getRoot()).progress.setProgress(0.1);
((Loading) Main.loading.getRoot()).statusTxt.setText("Seed created");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Random coord = new Random(seed);
position = new Coordinate((coord.nextInt() % 200) + 1, (coord.nextInt() % 200) + 1);
((Loading) Main.loading.getRoot()).progress.setProgress(0.2);
((Loading) Main.loading.getRoot()).statusTxt.setText("Starting coordinates instantiated");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
destStr = new ArrayList<Coordinate>();
consStr = new ArrayList<Coordinate>();
recStr = new ArrayList<Coordinate>();
builds = new ArrayList<Coordinate>();
((Loading) Main.loading.getRoot()).progress.setProgress(0.3);
((Loading) Main.loading.getRoot()).statusTxt.setText("Structure & Building list instantiated");