• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Java game loop problem with timimg

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bingo....

Exactly what I needed

Thanks so much

Elisheva
reply
    Bookmark Topic Watch Topic
  • New Topic