I'm creating a video game and I've got a for loop that recovers your health
public void recoverHealth() {
if (curHealth < finalHealth) {
for (double i = 0; i < finalHealth; i = i + 0.1) {
curHealth = curHealth + 0.1;
System.out.println("health: " + curHealth);
}
}
}
But the problem is that java goes through this so fast it goes from 0-20 before the game even starts. How can I possibly slow down the recoverHealth() method without slowing down the entire game such as Thread.sleep doesn't work..
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
Any ideas?
Put recoverHealth into a separate thread, and use Thread.sleep() as you describe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With