So I am making Plants vs Zombies in JavaFX. The best I could come up with for moving zombies and mowers etc was to execute each of them as thread. Now the issue is when do you check for collisions ? I tried using a while loop and it took forever and program just stopped. What should be condition for checking collision. Here is how I am doing:
while (true){
for (Zombie z : zombies[0]){
Shape a = Shape.intersect(z,mowers[0]);
if (a != null){
mowers[0].takeHit(1);
z.takeHit(100);
}
}
}
Issue is that zombie takes atleast 20 seconds to come till mower.
How do I use this loop more efficiently. As in how to replace the while loop more efficiently ?
For further reference, this is how zombie is moving
Thread.sleep(toStart*1000);
TranslateTransition translate = new TranslateTransition();
z.t = translate;
translate.setToX(-(1650+155));
translate.setDuration(Duration.seconds(30));
translate.setNode(z);
translate.play();
z.countdown.await();
node.remove.acquire();
node.root.getChildren().remove(z);
node.remove.release();