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 wanted to output a text for a certain amount of time. One way is to use threads. Are there any other ways? I can't use threads for slick2d. This is my code when I use threads for slick:

package javagame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import java.util.Random;
import org.newdawn.slick.Input;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.lwjgl.input.Mouse;

public class thread1 implements Runnable {
    String showUp;
    int timeLeft;

    public thread1(String s)
    {
        s = showUp; 
    }

    public void run(Graphics g)
    {
        try {
           g.drawString("%s is sleeping %d", 500, 500);
           Thread.sleep(timeLeft);
           g.drawString("%s is awake", 600,600);

        } catch(Exception e) { }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        run();
    }
}

It auto generates a new run() And also when I call it to my main class it has stack overflow!

share|improve this question
    
Umm.. Why would you weant to use threads? Can't you just make for example "TextPopup" class which contains timeLeft and text fields and a method to decrease the timeLeft and another one to draw the text. When timeLeft is under zero, just simply dont draw it anymore. Also, you are getting stackoverflow exception on that code because your run()-method is infinitely recursive. –  Jaakko Lipsanen Oct 5 '12 at 7:03
2  
Also, I doubt that many people will want to answer to your questions since your accept rate is 0%. –  Jaakko Lipsanen Oct 5 '12 at 7:09
    
Do you even listen for the previous answer you got? Your just totally wrong, stop writing random piece of code and post them here. Learn about thread, read comments/answers. Calling the run method in the run method will definitively raise a stack overflow as explained in one of your previous question. –  nathan Oct 5 '12 at 7:38
    
By the way, you don't have to use threads to achieve what you want. –  nathan Oct 5 '12 at 7:41
    
Like said before, store a variable with time left. When the text should be shown then set it to 1000, if you want to display it for a second. Every update, you subtract the delta time from the time left variable. If it reaches 0 or below, you can hide the text... –  tom van green Oct 5 '12 at 8:23
add comment

2 Answers 2

You can use the Timer class from LWJGL.

Somewhere in your code you would have:

Timer timer = new Timer();

Each frame you update this timer:

timer.tick();

And then when you need to retrieve the elapsed time:

float elapsed = timer.getTime() - lastframe;
lastframe = timer.getTime();

And do the processing you want:

if (elasped >= ACTION_DELAY)
    do();

This class also provides useful methods like pausing, resting etc.

Also you may want to check this.

share|improve this answer
    
This is slick not lwjgl. I don't have that import! –  SystemNetworks Oct 5 '12 at 8:52
    
@SystemNetworks then import it. Slick2D is built on top of LWJGL. You already import code from LWJGL... "import org.lwjgl.input.Mouse;" Just import the LWJGL package containing the Timer class. –  nathan Oct 5 '12 at 8:58
    
BTW. You can use any external library you want in your game. Your slick2d project is nothing but an simple java application. –  nathan Oct 5 '12 at 9:03
    
What's the import name? (etc. org.lwjgl.Time) –  SystemNetworks Oct 5 '12 at 13:48
1  
Check the javadoc for the Timer class. –  nathan Oct 5 '12 at 14:35
add comment

You need a long timeLeft not int

long = time int = just a number

share|improve this answer
1  
This does not really address the issue at all. Further, both 'long' and 'int' are numeric data types and longs are not necessarily only used for time representation. –  Josh Petrie Jan 7 at 18:09
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.