# animation runs only onces [closed]

I found this tutorial on sprite sheets that works great. How can I make a sprite-sheet-based animation system?

I am not sure how I could modify it, so that it loops through just once. My thought was to add an if statement in the update method. This way I could stop the animation, but I am unable to figure out what to put in the if statement.

public void update() {
if (!stopped) {
frameCount++;

if (frameCount > frameDelay) {
frameCount = 0;
currentFrame += animationDirection;

if (currentFrame > totalFrames - 1) {
currentFrame = 0;
if(){
stopped = true;
}
}
else if (currentFrame < 0) {
currentFrame = totalFrames - 1;
}
}
}

}

• – MichaelHouse Apr 15 '13 at 1:07
• " if()" IF WHAT? – Sidar Apr 15 '13 at 3:17
• @Sidar - "...but unable to figure it out what to put in if statment." – DMan Apr 15 '13 at 4:19
• @DMan yes but I was hoping he would come up with it himself – Sidar Apr 15 '13 at 5:28

## 2 Answers

I answered that post you are referring to... You need to create a Boolean like so:

private boolean loop;


And replace

If (currentFrame > totalFrames - 1)


With

   If (currentFrame > totalFrames - 1 && loop)


I'm on my phone at the moment. However I don't see why it wouldn't work.

You will need to set the Boolean to either true or false... You can easily do this in the constructor.

Basically it is saying if the current frame number is greater than total frames - 1 (total frame count) and loop = true, then make current frame = to 0 so the animation will restart...

Understand?

Good luck

• i tried it but it is not working. – jhon Apr 15 '13 at 16:17
• Ok lol... Leave it with me and when I get home from work ill test it for you and update my post :) – Savlon Apr 15 '13 at 20:05
• why is this close? – jhon Apr 16 '13 at 4:44

Add a boolean loop somewhere in the Animation class, then check if it's false.

• like this? <br/> 'boolean loop; if(!loop){ stopped = true; }' why its not tell me type? – jhon Apr 14 '13 at 23:51
• You'll have to set loop somewhere (perhaps in the constructor) to false or true. – DMan Apr 15 '13 at 0:02
• ah ok thanks man. just last question variable loop will be true if a animation you i want it to go for every. and false if i want animation to stop. how does java hows which animation is for ever and which is only onces. do just use 'totalFrame' variable to see which animation is being used? – jhon Apr 15 '13 at 0:28
• Unless I'm misunderstanding your question, the loop variable is what determines if it goes forever or not. – DMan Apr 15 '13 at 0:56
• yes, i can not set 'loop' in constructor to true or false right. i would need some kind of test which will tell 'loop' if its true of false. i was just confused on how to do it. do i use 'totalFrames' variable to test it? – jhon Apr 15 '13 at 1:16