Hello,
I recently started developing games in Java. But I cant get pass one problem. I searched the web and didnt find any answer, so I entered this forums.
My problem is the following: Thread.sleep(time) is not being as good as it should be. Here some example:
public static void main(String[] args) {
while (true){
sleep(20);
}
}
private static void sleep(int i) {
long startingtime = System.nanoTime();
try {Thread.sleep(i);}
catch (InterruptedException e) {e.printStackTrace();}
long endingtime = System.nanoTime();
double slepttime = (endingtime - startingtime ) /1000000.0;
System.out.println("Expected: "+i+" Real: "+slepttime);
}
On my Windows7 Pc I get results like:
Expected: 20 Real: 19.929772
Expected: 20 Real: 19.881132
Expected: 20 Real: 19.908117
Expected: 20 Real: 19.919111
Expected: 20 Real: 19.911116
Expected: 20 Real: 19.899788
Expected: 20 Real: 19.889128
Expected: 20 Real: 19.906784
But on WindowsXP Pc’s these results are really bad.
Ej:
Expected: 20 Real: 18,7034
Expected: 20 Real: 29.2780
Expected: 20 Real: 26.14
Expected: 20 Real: 20.1889
Expected: 20 Real: 27.4749
Expected: 20 Real: 16.6664
My question is: whats the method you guys use to make gameloop wait? Because Thread.sleep() doesn’t seem to be a really good way.
Thanks!