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.
-
What's your setup now? Are you using the [`Game`](http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/Game.html) and [`Screen`](http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/Screen.html) classes? – Steve Blackwell Jul 06 '12 at 21:14
-
Yes. A I have a class that deals with the logic of changing between Screens that implements Game. And 2 other classes that implement Screen(this is where I draw a Stage). – ruff1991 Jul 07 '12 at 11:04
3 Answers
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.

- 1,350
- 14
- 26
-
1I had to make 2 minor changes to this code run for me. `fadeOut(0.5f)` → `Actions.fadeOut(0.5f)`, and `run(new Runnable()` → `Actions.run(new Runnable()` – Stevoisiak May 12 '17 at 01:25
I've implemented some sliding transitions using Scene2D and the universal tween engine. You can find example code here.
http://www.netthreads.co.uk/2012/02/09/libgdx-scene2d-demo-with-scene-transitions/
Update: This article illustrates the approach I took to implement sliding transitions. There is a link at the bottom which takes you to a set of demos here https://github.com/alistairrutherford/libgdx-demos
There are clear instructions on how to build the demos but you will need to have at least a basic grasp of maven and how to set it up.

- 432
- 5
- 8
-
1Is this answer this valid as per today [Oct 2014] ? Spent last hours try to install the alistairrutherford/netthreads-libgdx with Maven but never managed to have anything working. Is there a walk-through trough the installation process ? – dawez Oct 20 '14 at 14:34
-
2And now the link seems to be dead. This is why you should never answer a question with a simple link. – Simon Forsberg Nov 04 '14 at 10:59
-
1Seems like it was just a temporary thing. I'm still not changing my vote because [it is still a link-only answer](http://meta.stackexchange.com/a/8259/213556) and you have not addressed the first comment by dawez. – Simon Forsberg Nov 04 '14 at 17:27
-
This doesn't answer the question, which was about `Screen`, not `Scene`. After digging through the linked source code, it turns out that `Scene` is just an extension of `Stage`, so isn't related to the concept of a libgdx `Screen`. See [here](https://github.com/alistairrutherford/netthreads-libgdx/blob/master/src/main/java/com/netthreads/libgdx/scene/Scene.java) – EntangledLoops Nov 16 '20 at 02:53
This is a sample code of my game in transitions animation between screens:
in MainGame
class:
@Override public void setScreen(final Screen screen) {
if (getScreen() == null)
{
createScreenInAction(screen);
return;
}
createScreenOutAction(getScreen(), new Runnable() {
@Override public void run() {
createScreenInAction(screen);
}
});
}
private void createScreenOutAction(final Screen screen , Runnable runnable) {
Actor actor = ((BaseScreenAdapter) screen).getStage().getRoot();
actor.setOrigin(WIDTH_HALF,HEIGHT_HALF);
actor.getColor().a = 1;
SequenceAction sequenceAction = new SequenceAction();
sequenceAction.addAction(Actions.parallel(Actions.alpha(0,SCREEN_SWITCH_DURATION) , Actions.scaleTo(1.5f,1.5f , SCREEN_SWITCH_DURATION, Interpolation.exp5)));
sequenceAction.addAction(Actions.run(runnable));
actor.addAction(sequenceAction);
}
private void createScreenInAction(final Screen screen) {
StarsGame.super.setScreen(screen);
Actor actor = ((BaseScreenAdapter) screen).getStage().getRoot();
actor.setOrigin(WIDTH_HALF,HEIGHT_HALF);
actor.getColor().a = 0;
SequenceAction sequenceAction = new SequenceAction();
sequenceAction.addAction(Actions.scaleTo(1.5f,1.5f , 0));
sequenceAction.addAction(Actions.parallel(Actions.alpha(1,SCREEN_SWITCH_DURATION) , Actions.scaleTo(1.0f,1.0f , SCREEN_SWITCH_DURATION , Interpolation.exp5)));
actor.addAction(sequenceAction);
}
and all my screens extends as BaseScreenAdapter
:
public abstract class BaseScreenAdapter extends ScreenAdapter implements BaseRequest.BaseResponseError{
protected final AssetsController mAssets;
protected final MySettings mSettings;
protected StarsGame mGame;
protected Stage mStage;
protected Viewport mViewport;
protected OrthographicCamera mCamera;
protected InputMultiplexer multiplexer;
protected LoadingActor mLoadingActor;
//==============================================================
// METHODS
//==============================================================
public BaseScreenAdapter(StarsGame game) {
this.mGame = game;
mCamera = new OrthographicCamera(/*StarsGame.WIDTH, StarsGame.HEIGHT*/);
mCamera.position.set(StarsGame.WIDTH_HALF, StarsGame.HEIGHT, 0);
mViewport = new FitViewport(StarsGame.WIDTH, StarsGame.HEIGHT, mCamera);
mCamera.position.set(mCamera.viewportWidth / 2f, mCamera.viewportHeight / 2 , 0);
initStage();
initInputMultiplexer();
}
public Stage getStage() {
return mStage;
}
private void initStage() {
mStage = new Stage(mViewport);
mStage.addListener(new InputListener() {
@Override public boolean keyUp(InputEvent event, int keycode) {
if (keycode == Input.Keys.BACK)
{
onBackPressed();
}
return super.keyUp(event, keycode);
}
});
}
}

- 1,699
- 1
- 19
- 23