LibGdx - Transition between Screens

ruff1991 picture ruff1991 · Jul 6, 2012 · Viewed 22.7k times · Source

I'm trying to change the animation between Libgdx Screens. I want to write my custom animation (fade in, fade out, etc). Can someone give me a clue? I can't seem to find the implementation of the transition in the Libgdx code.

Answer

Kedu picture Kedu · Jun 14, 2014

Here is what i do:

FadeIn is pretty simple, just add this to your fadein Screens show():

stage.getRoot().getColor().a = 0;
stage.getRoot().addAction(fadeIn(0.5f));

FadeOut is a little trickier. You dont want to switch screens immediately, so instead of calling game.setScreen(newScreen) create a method in your fadeout Screen like this:

public void switchScreen(final Game game, final Screen newScreen){
    stage.getRoot().getColor().a = 1;
    SequenceAction sequenceAction = new SequenceAction();
    sequenceAction.addAction(fadeOut(0.5f));
    sequenceAction.addAction(run(new Runnable() {
        @Override
        public void run() {
            game.setScreen(newScreen);
        }
    }));
    stage.getRoot().addAction(sequenceAction);
}

Like this, you delay the screen switching for the duration of the fade out.