1
\$\begingroup\$

I added some code using java.util.Timer to execute my spawnMonster function every 100ms. It worked, until I tried instantiating images in it -- since it doesn't run on the libGDX core thread, there's no OpenGL context, so it can't do stuff with images.

I figured using the libGDX Timer class, which the javadocs say runs on the core thread, would solve this problem; but unfortunately, the timer code just doesn't execute.

I tried:

- new Timer().scheduleTask(task, 0, 100) - new Timer().scheduleTask(task, 0, 100, 99999) - Timer.schedule(task, 0, 100) - Timer.schedule(task, 0, 999999) - Timer.start() - t = new Timer(); t.scheduleTask(...); t.start();

task appears to execute once, and only once; it never executes again. (It prints a date-time diff from a target time, so I can tell it's not running.)

\$\endgroup\$
2
  • \$\begingroup\$ Based on this libGDX test, new Timer(task, 1, 1) seems to work, but I wonder about the performance of this. \$\endgroup\$
    – ashes999
    Aug 3, 2014 at 1:12
  • \$\begingroup\$ Test #2: new Timer(task, 0, 1) also works, with the same caveats. \$\endgroup\$
    – ashes999
    Aug 3, 2014 at 1:19

1 Answer 1

2
\$\begingroup\$

The scheduleTask function's source suggests that it expects the input in terms of seconds instead of milliseconds.

public void scheduleTask (Task task, float delaySeconds, float intervalSeconds, int repeatCount) {
    if (task.repeatCount != CANCELLED) throw new IllegalArgumentException("The same task may not be scheduled twice.");
    task.executeTimeMillis = TimeUtils.nanoTime() / 1000000 + (long)(delaySeconds * 1000);
    task.intervalMillis = (long)(intervalSeconds * 1000);
    task.repeatCount = repeatCount;
    synchronized (tasks) {
        tasks.add(task);
    }
    wake();
}

The function converts intervalSeconds to milliseconds and sets the task's interval time to this value.

Therefore to have it execute every 100 ms, you should input 0.1 instead of 100.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .