Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I found this tutorial on sprite sheets that works great. Managing sprite sheets for animation

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) {

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

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

share|improve this question

closed as too localized by Byte56, bummzack, Anko, Tetrad Apr 15 '13 at 21:18

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

duplicate:… – Byte56 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

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)


   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...


Good luck

share|improve this answer
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
thanks so much. – jhon Apr 16 '13 at 1:03
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.

share|improve this answer
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

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