I am looking for a way to run my animation a complete cycle when a button is pressed. The animation runs as long as the button is pressed but the animation takes a second to play so it becomes too short. I would like to have it played through once, even if the button only was pressed very fast.

It's a character's slash with the knife and it needs to be one complete cycle, I just don't get how to do this.

Anyone has an idea on how I could do this in a better way?

The specific code is now inside render():

 if(Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
        if(direction == HEADING.NORTH) {
            animation = knifeslashnorth;
        } else if (direction == HEADING.WEST) {
            animation = knifeslashwest;
        } else if(direction == HEADING.EAST) {
            animation = knifeslasheast;
        } else if(direction == HEADING.SOUTH) {
            animation = knifeslashsouth;
  • \$\begingroup\$ You may want to post your entire main-application code, we can only guess right now what you already have. Here's the LibGDX wiki entry: github.com/libgdx/libgdx/wiki/2D-Animation \$\endgroup\$ – VaTTeRGeR Apr 28 '15 at 20:07
  • \$\begingroup\$ Are you using Scene2D with Actors? \$\endgroup\$ – StrongJoshua Apr 28 '15 at 21:18
  • \$\begingroup\$ @StrongJoshua Yes, orthogonal camera and im drawing the batch as usual. Maybe i just need to redo it in another way. \$\endgroup\$ – user1501127 Apr 29 '15 at 7:18
  • \$\begingroup\$ How are you rendering the animations? You should post at least that part of the Actor. \$\endgroup\$ – StrongJoshua Apr 29 '15 at 20:26
  • \$\begingroup\$ what is happening (or what are you expecting to happen) when the user play too fast two different directions? Are you expecting to animate both animations? Just one? cancel one then animate the other? \$\endgroup\$ – Alex Sifuentes Jul 16 '15 at 15:50

I think it will work if you combine your input listener with:


While playing animation ignore attack input and let it just continue playing. When the animation finishes you can respond again and start a new animation by reseting the stateTime.

But I would also have in mind that the player can move meanwhile and there you have 2 options:

  • Allow movement and cancel animation when direction changes.
  • Also ignore directional changing input until animation finishes.

I hope it helps!


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.