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?
将recoverHealth
放入一个单独的线程中,并按照您的描述使用Thread.sleep()
。
Add this to your game program, where curHealth is accessible.
timer = new Timer();
timer.schedule(new TimerTask (){
public void run() {
curHealth = curHealth + 0.1;
}
}, 1000);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.