Search code examples
libgdx

Libgdx: Pause between one cycle of animation


I have animation with 5 frames. I want to make pause for x seconds every time one animation cycle ends

1,2,3,4,5 (pause) 1,2,3,4,5 (pause) ...

    Array<AtlasRegion> regions =  atlas.findRegions("coin");
    animGoldCoin = new Animation(1.0f / 20.0f, regions, PlayMode.LOOP);

I can't find way to do this.

Thanks


Solution

  • I don't really like the animation class, you can make your own.

        float pauseTime=5f; // lets say that you want to pause your animation for x seconds after each cicle
    
    float stateTime=0f; // this variable will keep the time, is our timer
    float frameTime=1/20f; //amount of time from frame to frame
    int frame=0; // your frame index
    boolean waiting=false; // where this is true, you wait for that x seconds to pass
    
    void Update()
    {
        stateTime+=Gdx.graphics.getDeltaTime();
        if(!waiting && stateTime>frameTime) // that frame time passed
        {
            stateTime=0f; // reset our 'timer'
            frame++; // increment the frame
        }
        if(waiting && stateTime>=0)
        {
            frame=0;
            waiting=false;
        }
        if(frame>=NUMBER_OF_FRAMES)
        {
            frame--;
            waiting=true;
            stateTime=-pauseTime;
        }
    }
    
    
    }
    

    I think this will work, do you understand what I did?