[LibGDX] Problem with Univesal Tween Engine

I have simple shrinking effect in my game and it works perfectly the way it should on Desktop but when I debug it or test it on my phone then the effect is super fast. The object which should shrink from scale A to B in three seconds instead shrinks in blink of an eye…

Any idea what I’m missing or doing wrong?

Code?

Here is the effect (Effects.java)


public static void init() {
        Tween.registerAccessor(Sprite.class, new SpriteAccessor());
        Tween.registerAccessor(Label.class, new FontAccessor());
        spriteManager = new TweenManager();
        fontManager = new TweenManager();
    }

public static void updateSpriteManager(float delta) {
        spriteManager.update(delta);
    }        

public static void shrink(final Sprite sprite, int scaleXY, float duration){
Tween
                .to(sprite, SpriteAccessor.SCALE, duration)
                .target(scaleXY)
                .start(spriteManager); 
}


And I use it on GameScreen.java’s show method


Effects.init();
Effects.shrink(centerObject, 1, 3f);

And on the render method in GameScreen.java I have


Effects.updateSpriteManager(delta);

But when I run it on Desktop it works perfectly. The problem occurs only when running on mobile so I’m thinking that there shouldn’t be any flaw in the code? Maybe?

The flaw is always in the code ;), even if you are evidently not doing anything wrong.

Unless it’s a bug in the UTE, I suspect that the delta passed to updateSpriteManager() is higher than you expect it, probably due to the time spend in init(), since the phone is probably way less powerful than your Desktop and initializing two TweenManagers might take a while.

Try measuring the delta passed to the updateSpriteManager() and let the system “swing in”, before you are trying the animation.

Where are you getting the delta from? I assume from the Gdx.graphics.getDeltaTime()? The documentation reads the time span between the current frame and the last frame in seconds. Might be smoothed over n frames, so it might still recover from initialization load.

@Kami

Assuming the documentation I found is the same, the parameter is in millis, not seconds.

http://www.aurelienribon.com/universal-tween-engine/javadoc/aurelienribon/tweenengine/Tween.html#to(java.lang.Object,%20int,%20int)

Sorry my late response I’ve been busy with work lately. So yeah, I printed the delta value (Gdx.graphics.getDeltaTime) and in Desktop the first two values are around 0.4-0.06 and after that it’s around 0.016.

On mobile however the first five values are around 0.8 and after that it’s the same as in Desktop mode 0.016.

So it’s the delta? Like I previously stated. The problem occurs when changing from MainMenuScreen to GameScreen and the first effect (the shrink) happens in a blink of an eye. The effects after that (In-game effects) works perfectly.

Any idea on how to fix this?

The problem is, that the engine is under load so much, that the time it takes to render a frame is this long. To keep constant duration of phases in a game, the delta is therefor increased. This is called variable timestep. The downside of this is skipped frames of animations. If the skipped frame is at the beginning of an animation, it appears to be shortened like in your case.

There are two possible solutions to this:

  1. you could optimize your code to not have this peaks of load, but have the demand spread evenly in your game. This includes profiling for bottlenecks and refactoring your code accordingly. At some places you might have to wait some frames before playing the anmation after some heavy load was lifted.

  2. you could switch to fixed timestep, where you assume a certain constant frame time and make sure by sleeping appropriately that the delta is not shorter than your assumed frametime. This has the downside of animations slowing down on long frame deltas, but is usually easier to handle and might be required for physics based animations, to not have your sprites fly of in all directions, when the delta is unexpectedly long or short.

One thing you should check for sure is, if you are doing redundant stuff between frames, like initializing a new TweenEngine every animation instead of reusing one you create at the beginning of the game.

Yup. The problem is solved :slight_smile: Thanks for your help!

Please explain what you did to solve it and what was the root cause, so others can benefit as well, thanks!

Well, I did as you suggested. I optimized my code so that before the effects start running, the game has loaded every needed assets so it doesn’t mess up with the effects.