Successive Thread.sleep() keeps on running ideally delayed code in one synchronized instant

Viewed 62

I am currently working with JavaFX on a game program. What I have planned here is a loading panel that in ideal sequence:

  1. switches the main stage to the loading scene
  2. waits for 1s
  3. sets visible a panel that shows the loading status
  4. waits for another 1s
  5. 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:

  1. waits for 4~5s

  2. 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");
        
0 Answers
Related