0
\$\begingroup\$

So, I've got some animations in LibGDX that I want to get running. Currently, the walking animations work perfectly, but attacking doesn't. I want the attack animation to play once, then set the State back to WALKING.

I've been experimenting with attacking upward (that's why the code's different), and all it does is flash and go back to walking. The other three directions show only the last frame of the animations, and holds it there. Does anyone know how to get this to work? I've looked at the similar questions, but none of them are helping.

There's 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;
    }
\$\endgroup\$
  • \$\begingroup\$ never mind, I actually got it; will post code later \$\endgroup\$ – ND523 Feb 17 '20 at 21:40
  • \$\begingroup\$ Interesting, would love to know how you fixed it \$\endgroup\$ – Luis Fernando Frontanilla Feb 22 '20 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.