1
\$\begingroup\$

i have an issue that i hope it's easy to resolve. I'm playing various animations in my libgdx-created game.

For a particular animation, i need that to play in loop mode but only if a particular condition is true. Simple this far.

What i need is to "reset" the animation (i mean restart from sprite n°1) when the condition become true again after the animation stopped. I know that i can simply dispose the actual animation and create another one every time the condition is true, but this seems a complicated way to resolve this..

How do i change the value of the index used by that animation? Is it possible? Do i have to create a class that extends the Animation class from libgdx library?

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

Try only updating the state time of the animation when the condition is true, and then when it goes from false to true and the animation needs to re-start, set the state time to 0 again.

float stateTime;

void render() {
    if(condition) {
        stateTime += Gdx.graphics.getDeltaTime();
    }

    frame = animation.getKeyFrame(stateTime, true);
}

void setCondition(boolean v){
    if (v && !condition)
        stateTime=0;

    condition = v;
}
\$\endgroup\$
0
0
\$\begingroup\$

if you have an animation you have to give it a time, probably you give it your deltaTime but its wrong. You need a animTime, in your render you will make animTime+=deltaTime. When you want to reset simply set animTime to 0

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .