Search code examples
javaandroidlibgdx

libGDX pause running for some seconds


I have an application, in which I would like to make a short pause. It would be important, to make graphical elements/changes better visible between two automatic interactions.

I have tried to use above code above for 2 minutes of pause, but it causes a pause at the start of the method, not between 2 exact lines of code inside the method.

try {
    Thread.sleep(2000);
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}

Solution

  • Libgdx expects the render loop to run regularly (at roughly the screen refresh rate) and to re-draw the screen each time. As, such while your app may "look" paused, it should not actually be paused. It should be busy updating the screen, updating animations, being responsive to the back key, etc.

    So, you need to decide more explicitly what is being prevented during this window. Just responding to input? Adding new elements? Then you should skip those elements during your window.

    long endPauseTime = 0;
    

    When you decide you want to pause:

    endPauseTime = System.currentTimeMillis() + (2 * 1000); // 2 seconds in the future
    

    Then in your render calback:

    if (endPauseTime < System.currentTimeMillis()) {
       // non-paused mode, so check inputs or whatever
    } else {
       // paused mode, so skip some things ..
    }