Jazib
Jazib

Reputation: 1230

Libgdx Animation run loop only twice

So I'm trying to stop an animation after running twice. I am playing the animation by setting

Animation blink = new Animation(BLINK_TIME, frames); // parameters are my fields

This is how i get my current frame:

   if(animate){
      stateTime += Gdx.graphics.getDeltaTime(); //stateTime was initialized to 0
      currentFrame.setRegion(blink.getKeyFrame(stateTime, true)); 
}
else 
   currentFrame.setRegion(defaultRegion);  // again, my own field!

Everything works fine uptill here when the animate field is true. Now I want to stop the animation, this is what I do for it:

if(blink.isAnimationFinished(stateTime)){
        blinkTimes++;
        if(blinkTimes>=2)
            animate = false;
    }

The problem is, isAnimationFinished(stateTime) never gets called. I guess it has to do something with looping animation. Can someone please point me in the right direction? Thanks!

Upvotes: 0

Views: 4400

Answers (2)

Jazib
Jazib

Reputation: 1230

Found the Problem!

isAnimationFinished(stateTime) does not work when animation is being played in the loop. So, I had to play it in Animation.Normal mode and use false in blink.getKeyFrame(stateTime, false) telling the Animation class to run it in non-loop mode!

Upvotes: 3

Paras Mittal
Paras Mittal

Reputation: 1149

Everything seems ok. I think you have initialized

stateTime = 0;

inside the render method or update method where firstly its value = 0 and then you add deltaTime to it.. In the end it never reaches to position where animation gets finished. I think you should log the value of stateTime when your

if(animate) // log what is your stateTime

And 1 more thing

  if(blink.isAnimationFinished(stateTime)){
    blinkTimes++;
     stateTime = 0 ; //----------- because your animation should start from begining else it will complete 2 cycles in no time.
    if(blinkTimes>=2)
        animate = false;
}

I hope this helps.. and if i am not getting your question properly just let me know.

Upvotes: 1

Related Questions