Pages

Tuesday, September 3, 2013

Adding 15-second timer to your game's splash screen (libgdx)

Adding a timer to your game is quite simple.

Here we call the schedule() method of the Timer object and pass it a new Task object and delay parameter (15 seconds in this case). The run() method will fire after the 15 seconds and inside it you define what to do. In this case we just call another method that handles the change to another screen.

We also set touch event because usually the player doesn't want to hang on the splash screen:

private boolean timerIsOn = false;

@Override 
public void render() {
   Gdx.gl.glClearColor(0, 0, 0, 1);
   Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

   
      if(!timerIsOn) {
         timerIsOn = true;
         
         Timer.schedule(new Task() {
            
            @Override
            public void run() {
               changeScreen();
            }

         }, 15);
            
      } else if(Gdx.input.isTouched()) {
           // Remove the task so we don't call changeScreen twice:
           Timer.instance().clear();
           changeScreen();
      }
}


Check also the Timer class javadoc for more information and methods.