Jump to content
  • Advertisement
Sign in to follow this  
Eli2007

[java] Help with Thread.Sleep() after render

This topic is 4053 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I am working on a game where objects are scrolling down and up the display at fixed rate. So my "run" method looks like: I use the Sleep because although I am incrementing by 1 each time, it is very fast movement and I want it to be slower, or at least something which I can set in my game i.e. easy level = sleep more, advance level = sleep less = move faster. When objects are on the same Y index in the display I want the animation to freeze for more time, so the player can check this situation. So I tried to use two sleep_time values. One for regular small time sleep and the other for long period of sleep. The problem, it does not work. My guess is I am not using the sleep function in a right way. Can any one help ???? Thanks public void run() { long excess = 0L; long wait_time = 0; running = true; while(running) { wait_time = gameUpdate(); gameRender(); paintScreen(); if (wait_time == 1) sleepTime = 5000L; else sleepTime = 200L; wait_time = 0; if (sleepTime > 0) try { // Thread.sleep(sleepTime/100000L); // nano -> ms Thread.sleep(sleepTime); // nano -> ms } catch(InterruptedException ex) { ex.printStackTrace(); System.exit(1); } } // while System.exit(0); // so window disappears } // end of run()

Share this post


Link to post
Share on other sites
Advertisement
Try replacing the thread.sleep with a Object.wait. The reason why you do this is because your not specifying a thread with the other method so you might not be making the right thread sleep when you want it to. If you need to wake it up early just do a waiter.notify(). synchronized is fine because when the call to wait is done, the code block releases its lock.


Object waiter;
synchronized (waiter) {
try {
waiter.wait(100);
} catch (Exception ex) {}
}


Share this post


Link to post
Share on other sites
Hi,

I have one thread which is the animator. I worked it out by assigning values to sleep based on x and y coords.

Tnx

Elisheva

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!