I am making a game, what should happen is, inside a for loop while iterating over an arraylist. Inside each loop I want to add a node to my scene, then I want to wait some time and add another node to the scene. the time between each iteration is also defined in the item of the arraylist, and can be different each iteration.
What i've tried:
//Circle is my own class, other than these attributes along with getters it has nothing in it.
//The last number is the time to wait.
//Also for testing i used a static arraylist, normally the items come from a json file.
ArrayList<Circle> lvlNodes = new ArrayList<>();
lvlNodes.add(new Circle(50,50, Color.RED,250,100));
lvlNodes.add(new Circle(200,100, Color.BLUE,500,250));
lvlNodes.add(new Circle(900,500, Color.YELLOW,750,500));
lvlNodes.add(new Circle(400,50, Color.GREEN,1000,500));
//Iterating over the arraylist and adding the nodes.
for (int i=0; i<lvlNodes.size(); i++) {
Circle currentNode = lvlNodes.get(i);
//Add the node the the anchor pane.
view.addLvlNode(currentNode, diameter); //diameter is a final value, all nodes in the game are the same and thus only added once at the top of the json file
//Wait and move on to the next one.
try {
Thread.sleep(currentNode.getTimeToNextNode())
} catch (InterruptedException e) {
System.out.println(e);
}
}
//Adds the next node to the game.
public void addLvlNode(Circle circle, double diameter) {
//Create node.
javafx.scene.shape.Circle lvlNode = new javafx.scene.shape.Circle(circle.getPosX(),
circle.getPosY(), diameter/2);
//Anchor node the the anchorpane.
AnchorPane.setTopAnchor(lvlNode, (double)circle.getPosY());
AnchorPane.setLeftAnchor(lvlNode, (double)circle.getPosX());
anchrLevel.getChildren().add(lvlNode);
lvlNode.toBack();
}//addLvlNode.
The Thread.sleep() works, each iteration in the for loop is with the time in between. but the nodes don't get added until the for loop is done iterating.
Is there any way of adding the nodes inside the for loop with the time amount in between?