| Author |
Java game loop problem with timimg
|
Elisheva EshkarBarAm
Greenhorn
Joined: Apr 30, 2007
Posts: 3
|
|
Hi all, I am developing a java game. where after each move of a sprite I want it to rest for x seconds (using sleep method). Each 50th move I want it to rest for more. i.e. the sprite walks and rests, walks and rests. This is how my code looks: public void run() /* The frames of the animation are drawn inside the while loop. */ { int noDelays = 0; long excess = 0L; long wait_time = 0; running = true; while(running) { gameUpdate(tick); gameRender(); paintScreen(); if ((tick % 46) == 0) wait_time = 1000; else wait_time = 80; System.out.println("wait time " + wait_time + " tick " + tick); try { Thread.sleep(wait_time); // sleep a bit } catch(InterruptedException ex){} tick++; } } I am getting this output from the System.out.println("wait .... call: wait time 1000 tick 0 wait time 1000 tick 0 wait time 80 tick 2 wait time 80 tick 2 wait time 80 tick 4 wait time 80 tick 4 wait time 80 tick 6 wait time 80 tick 6 wait time 80 tick 8 wait time 80 tick 8 wait time 80 tick 10 wait time 80 tick 10 wait time 80 tick 12 wait time 80 tick 12 wait time 80 tick 14 wait time 80 tick 14 wait time 80 tick 16 wait time 80 tick 16 wait time 80 tick 18 wait time 80 tick 18 wait time 80 tick 20 wait time 80 tick 20 wait time 80 tick 22 wait time 80 tick 22 wait time 80 tick 24 wait time 80 tick 24 wait time 80 tick 26 wait time 80 tick 26 wait time 80 tick 28 wait time 80 tick 28 wait time 80 tick 30 wait time 80 tick 30 wait time 80 tick 32 wait time 80 tick 32 wait time 80 tick 34 wait time 80 tick 34 wait time 80 tick 36 wait time 80 tick 36 wait time 80 tick 38 wait time 80 tick 38 wait time 80 tick 40 wait time 80 tick 40 wait time 80 tick 42 wait time 80 tick 42 wait time 80 tick 44 wait time 80 tick 44 wait time 1000 tick 46 wait time 1000 tick 46 wait time 80 tick 48 wait time 80 tick 48 wait time 80 tick 50 I don;t understand why each line is printed out twice ??. Plus the animation don't always follows the wait time. Some times it rests and other times it keeps moving. Any idea how to implement this movement ??? Thanks Eli7
|
 |
Charles Rhinehart
Greenhorn
Joined: May 27, 2007
Posts: 8
|
|
|
imho I would let the thread run at full speed; capture the time delta and pass that into your object representing the sprite -- I'm guessing your sprite object has a 'speed' variable that you can use to determine how quickly the sprite updates it's x, y, (z?) position based on the time... just my two cents.
|
 |
Elisheva EshkarBarAm
Greenhorn
Joined: Apr 30, 2007
Posts: 3
|
|
Thanks for replying. Not sure I can do that. These loops dont do good to the cpu.... The problem is to implement a Thread.sleep with variable sleep time... Have you seen an example code for this somewhere ??? Thanks Elisheva
|
 |
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
|
|
why each line is printed out twice...Some times it rests and other times it keeps moving Maybe there are two threads running. For the counting by twos, maybe tick is being incremented before this call gameUpdate(tick); returns, ie, outside the run method.
|
 |
Elisheva EshkarBarAm
Greenhorn
Joined: Apr 30, 2007
Posts: 3
|
|
Bingo.... Exactly what I needed Thanks so much Elisheva
|
 |
 |
|
|
subject: Java game loop problem with timimg
|
|
|