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

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

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

  • 2
    \$\begingroup\$ duplicate: gamedev.stackexchange.com/questions/46756/… \$\endgroup\$
    – House
    Commented Apr 15, 2013 at 1:07
  • 1
    \$\begingroup\$ " if()" IF WHAT? \$\endgroup\$
    – Sidar
    Commented Apr 15, 2013 at 3:17
  • \$\begingroup\$ @Sidar - "...but unable to figure it out what to put in if statment." \$\endgroup\$
    – DMan
    Commented Apr 15, 2013 at 4:19
  • \$\begingroup\$ @DMan yes but I was hoping he would come up with it himself \$\endgroup\$
    – Sidar
    Commented Apr 15, 2013 at 5:28

2 Answers 2


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

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

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

  • \$\begingroup\$ like this? <br/> 'boolean loop; if(!loop){ stopped = true; }' why its not tell me type? \$\endgroup\$
    – jhon
    Commented Apr 14, 2013 at 23:51
  • \$\begingroup\$ You'll have to set loop somewhere (perhaps in the constructor) to false or true. \$\endgroup\$
    – DMan
    Commented Apr 15, 2013 at 0:02
  • \$\begingroup\$ 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? \$\endgroup\$
    – jhon
    Commented Apr 15, 2013 at 0:28
  • \$\begingroup\$ Unless I'm misunderstanding your question, the loop variable is what determines if it goes forever or not. \$\endgroup\$
    – DMan
    Commented Apr 15, 2013 at 0:56
  • \$\begingroup\$ 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? \$\endgroup\$
    – jhon
    Commented Apr 15, 2013 at 1:16

Not the answer you're looking for? Browse other questions tagged .