java – LibGDX Box2D, how to play an animation once, then reset


So, I have some animations in LibGDX that I want to launch. Currently, the walking animations work perfectly, but the attack does not work. I want the attack animation to be played once, then reset the state to WALKING.

I experienced the upward attack (that's why the code is different), and all it does is flash and go back to walking. The other three directions only display and maintain the last frame of the animations. Does anyone know how to make this work? I have looked at similar questions, but none of them help.

There is a lot of code in this method, so I apologize in advance:

private TextureRegion getFrame(float dt) {
        TextureRegion region = null;
        if (currentState == State.WALKING) {
            switch (currentDir) {
                case UP:
                    if (b2body.getLinearVelocity().x == 0 && b2body.getLinearVelocity().y == 0) {
                        stateTimer = 0;
                        region = heroWalkUp.getKeyFrame(stateTimer, false);
                        stateTimer = 1;
                    } else {
                        region = heroWalkUp.getKeyFrame(stateTimer, true);
                    }
                    break;
                case RIGHT:
                    if (b2body.getLinearVelocity().x == 0 && b2body.getLinearVelocity().y == 0) {
                        stateTimer = 0;
                        region = heroWalkRight.getKeyFrame(stateTimer, false);
                        stateTimer = 1;
                    } else {
                        region = heroWalkRight.getKeyFrame(stateTimer, true);
                    }
                    break;
                case LEFT:
                    if (b2body.getLinearVelocity().x == 0 && b2body.getLinearVelocity().y == 0) {
                        stateTimer = 0;
                        region = heroWalkLeft.getKeyFrame(stateTimer, false);
                        stateTimer = 1;
                    } else {
                        region = heroWalkLeft.getKeyFrame(stateTimer, true);
                    }
                    break;
                case DOWN:
                default:
                    if (b2body.getLinearVelocity().x == 0 && b2body.getLinearVelocity().y == 0) {
                        stateTimer = 0;
                        region = heroWalkDown.getKeyFrame(stateTimer, false);
                        stateTimer = 1;
                    } else {
                        region = heroWalkDown.getKeyFrame(stateTimer, true);
                    }
                    break;
            }
        } else if (currentState == State.ATTACKING) {
            switch (currentDir) {
                case UP:
                    region = heroAtkUp.getKeyFrame(stateTimer, true);
                    if (heroAtkUp.isAnimationFinished(stateTimer)) {
                        currentState = State.WALKING;
                    }
                    break;
                case LEFT:
                    region = heroAtkLeft.getKeyFrame(stateTimer);
                    break;
                case RIGHT:
                    region = heroAtkRight.getKeyFrame(stateTimer);
                    break;
                case DOWN:
                    region = heroAtkDown.getKeyFrame(stateTimer);
                    break;
                default:
                    region = heroWalkDown.getKeyFrame(stateTimer); //should never happen
                    break;
            }
        }

        stateTimer = currentDir == previousDir ? stateTimer + dt : 0;
        previousState = currentState;
        previousDir = currentDir;
        return region;
    }