Libgdx - changing tiledmap smoothly, Tween engine maybe?

First question here, so bare with me if I make a mistake or two. Long story short I'm currently developing a side scroller game in java with libgdx, which uses tiledmaps from the tiledmap creator software. When the player has run for some time the map is automaticly changed to a new one, using some code which looks similar to this:

void changeMap() { Gdx.app.postRunnable(() -> { testMap = new TmxMapLoader().load("someMap.tmx"); renderer.getMap().dispose(); renderer.setMap(testMap); }); }

My problem is that the map is changed instantly with no smooth transition at all, like one sec the player is on the old map, the next he starts instantly on the new one, which is quilte unprofessional in My opinion. I would like to create some smooth transition between the two. Maybe just a simple fade in, fade out. Any thing is Better than how it is now.

I have searched google, tried different "solutions" using the tween engine, tried Making a transition screen which first fades in over the playscreen, but when i try to fade back the playscreen is just stoppede etc. I'm really emptied for ideas, so it would be awesome if one of you experts could point me in the right direction. Or maybe Come up with a simple solution. I am experienced with java so any idea here will be apriciated.

This is written from a phone, so sorry for any spelling mistakes.

1 Answer

I would agree with using the TweenEngine. It's relatively simple to use and provides a lot of functionality. To tween a value you first have to create a TweenAccessor. It would look something like this for a Sprite:

public class SpriteAccessor implements TweenAccessor<Sprite> {

//Used to identify what type of tweening you wish to do.
public static final int ALPHA = 0;

@Override
public int getValues(Sprite target, int tweenType, float[] returnValues) {
switch (tweenType) {
case ALPHA: //If the tween type is ALPHA as defined above...
//Set the returnValues to the alpha of the sprite.
//returnValues is the array of values we wish to tween (change).
returnValues[0] = target.getColor().a;
//Then return the number of values you changed in the array.
return 1;
default:
assert false;
return -1;
}
}

@Override
public void setValues(Sprite target, int tweenType, float[] newValues) {
switch (tweenType) {
case ALPHA:
Color color = target.getColor(); //Get the sprite color
//Here we set the alpha to the tweened alpha value we entered
//above.
target.setColor(color.r, color.g, color.b, newValues[0]);
break;
default:
assert false;
}
}

}


Then you would use it like this:

Tween.to(yourObject, SpriteAccessor.ALPHA, anyDuration).target(anyAlphaValue).start(yourTweenManager);
//Changes the alpha value of yourObject from it's current value to anyAlphaValue.


Of course this TweenAccessor only works for Sprites. You'd have to write your own for a TiledMap, or for whatever object you wish to tween. I can't give en example of a TiledMap alpha tween right now as I don't know from the top of my head how to change the color of a TiledMap, but I'm sure you can figure that out yourself.

• Thank you for your response. I am however quite familar with the tween engine, so already have this kind of accessor in my project. But I have now found a solution to my problem. Since I'm using States to control when to update certain functions I just created a Transition state, which works flawlessly ! Thank you anyway :) – Klatzen May 14 '16 at 21:38
• I see :) glad you found a solution! – Charanor May 14 '16 at 21:39