1

I'm programming a game in Java slick and I want to add a message management. There should be a ArrayList which contains all not yet sent messages. Every 2 seconds the next item in the list should be displayed.

My logic:

In the update methode the first message should be displayed. After 2 seconds this item gets removed.

My problem is that just the first one is displayed and the others not. When I was debugging I noticed that after these 2 seconds the timer continues to remove the first item without a 2 second break.

Please can you help me to find the problem or a better logic.

Here is some of my coding.

Mothode to put a message into the queue

public static void showMessage(String message){
    msgHandler.addMessage(message);
}

Render methode

public void render(GameContainer gameContainer, Graphics g) throws SlickException {
    msgHandler.getCurrentMessage().displayMessage(g);
    if(msgHandler.isTimerCompleted()){
        msgHandler.next();
    }
}

Message manager class

public class MessageHandler {
    private ArrayList<Message> messages;
    private int duration = 2000;
    private Timer timer = new Timer();
    private boolean isTimerCompleted = true;

    public MessageHandler() {
        messages = new ArrayList<>();
    }

    public void next() {

        isTimerCompleted = false;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (messages.size() > 0) {
                    messages.remove(0);
                }
                System.out.println("test");
            }
        }, duration);
        isTimerCompleted = true;
    }

    public Message getCurrentMessage() {
        if (messages.size() > 0)
            return messages.get(0);
        return new Message("");
    }

    public void addMessage(String message, int x, int y) {
        messages.add(new Message(message, x, y));
    }

    public void addMessage(String message) {
        messages.add(new Message(message));
    }
}
Gaëtan Maisse
  • 12,208
  • 9
  • 44
  • 47
Lorenz
  • 67
  • 6

1 Answers1

0

It looks like you want to use a recurring timer like the example below. Also, timer.schedule does not pause execution until the timer is over, instead, when the time limit is up, the run() part of your timer is executed. Currently, isTimerCompleted = true; will be executed as soon as you create your timer.

timer.scheduleAtFixedRate(new TimerTask() {
  @Override
  public void run() {
    // Your database code here
  }
}, 2*60*1000, 2*60*1000);

A good overview is here: https://stackoverflow.com/a/4044793/1320066

EDIT:

To be clearer, "isTimerCompleted = true" is being executed as soon as your timer task has been created, so you are effectively continuously creating new TimerTasks. Try moving "isTimerCompleted = true;" to inside of the "public void run() { .. }" block of code.

For an example of what is happening, try running this simple section of code to see in what order your statements are being executed:

import java.util.TimerTask;
import java.util.Timer;

public class TimerSchedule {

    public Timer timer = new Timer();

    public void TestTimerSchedule() {
        System.out.println("Before timer.schedule");
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("TimerTask");
            }
        }, 2000);

        System.out.println("After timer.schedule");
    }

}

Output:

Before timer.schedule
After timer.schedule
TimerTask
Community
  • 1
  • 1
NickD
  • 506
  • 5
  • 15
  • I think you understood this wrong. Every time the functions gets called there should be a 2 second delay and then the message gets removed from the list. My problem is, that after the timer finished for the first time, everything in the run methode gets executed continuously. It's looping. But I want that the run methode is not a endless loop. – Lorenz Feb 24 '17 at 11:51
  • Hi, I've updated my answer to be a bit more specific, please let me know what you think. – NickD Feb 24 '17 at 12:45