# Run one cycle of animation when button is pressed in libgdx

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;
}
}

• 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 – VaTTeRGeR Apr 28 '15 at 20:07
• Are you using Scene2D with Actors? – StrongJoshua Apr 28 '15 at 21:18
• @StrongJoshua Yes, orthogonal camera and im drawing the batch as usual. Maybe i just need to redo it in another way. – user1501127 Apr 29 '15 at 7:18
• How are you rendering the animations? You should post at least that part of the Actor. – StrongJoshua Apr 29 '15 at 20:26
• 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? – Alex Sifuentes Jul 16 '15 at 15:50

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

animation.isAnimationFinished(stateTime);


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!