Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I wan tto give delay in java without using thread. I need to load images one by one with approx half second delay. Is there any way to do this other than thread because if I am using thread my whole game is going into the sleep mode.

share|improve this question

3 Answers 3

Correct solution should be to use another thread for loading images. And the waiting aprox half second is not good. Your image loading function should have some callback to tell the loading thread that the image is loaded.

share|improve this answer

you said that you don't want to use "thread" because your whole game sleeps. I think you are referring to Thread.sleep(). This is just one static method in the Thread class. The main purpose of the Thread class is to allow parallel execution. So the idea is to execute the image loading code in parallel to your normal processing.

Thread imageLoadingThread = new Thread(new Runnable() {
    public void run() {
        // loop to load image
            // load image
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        // end loop
    }
});
imageLoadingThread.start();

The Thread.sleep(500) call will now only put the new image loading thead to sleep while the rest of the game continues to be executed.

Note: I assumed that you need the delay in order to keep load on the server low. If the delay is needed in order to wait until the image is loaded completely on the client side, a different approach would be better.

share|improve this answer

What are you trying to achieve? Can you only load the next image if the previous one has finished loading?

You can track the time when the last image was loaded and then check in your game update function if enough time since the last loaded image has passed.

Your game stalls when using sleep because you pause your main thread. As nhnb said you need to write seperate image loader thread class.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.