i have a enemy and spriteanimation classes. in Enemy class i have bufferedimage array with 4 different images. than i am creating a animation and storing in 'animationEnemyHit' variable. than i am starting and updateing the animation in method. the problem is that it keeps on loop for ever. i want it stop after one full cirlce. so after 4th image stop.

public class Enemy{
   private BufferedImage[] bEnemyhit = {SpriteSheet.getSprite(6, 0),         SpriteSheet.getSprite(7, 0), 
                                     SpriteSheet.getSprite(8, 0),     SpriteSheet.getSprite(9, 0),};

private SpriteAnimation aEnemyHit = new SpriteAnimation(bEnemyhit, 10, false);     //set loop vairable in spriteAnimation.class to false
private SpriteAnimation animationEnemyHit = aEnemyHit;
private boolean flag = false;
...
public void enemyMove()    //this method is in main game loop
{
    x -= dx;
    if(!flag){            //start only onces
        this.animationEnemyHit.start();
        flag = true;
    }
    this.animationEnemyHit.update();

} 
...
 }

SpriteAnimation.class i am setting 'loop' vairable to true or false(value coming from enemy.class). if its false than it should not loop for ever so stop after one circle. in update method i did set up a if statment but it is not working. it just keep on looping.

public class SpriteAnimation {

private int frameCount;                 // Counts ticks for change
private int frameDelay;                 // frame delay 1-12 (You will have to play around with this)
private int currentFrame;               // animations current frame
private int animationDirection;         // animation direction (i.e counting forward or backward)
private int totalFrames;                // total amount of frames for your animation

private boolean stopped;                // has animations stopped

private boolean loop;                  //loop for ever or loop onces

private List<SpriteFrame> SpriteframeObject = new ArrayList<SpriteFrame>();    // Arraylist of frames 

public SpriteAnimation(BufferedImage[] frames, int frameDelay, boolean shouldLoop) {
    this.frameDelay = frameDelay;
    this.stopped = true;
    this.loop = shouldLoop;   //see if it has to loop or no

    for (int i = 0; i < frames.length; i++) {
        addFrame(frames[i], frameDelay);
    }

    this.frameCount = 0;
    this.frameDelay = frameDelay;
    this.currentFrame = 0;
    this.animationDirection = 1;
    this.totalFrames = this.SpriteframeObject.size();
}




public void start() {
    if (!stopped) {
        return;
    }

    if (SpriteframeObject.size() == 0) {
        return;
    }
    stopped = false;
}

public void stop() {
    if (SpriteframeObject.size() == 0) {
        return;
    }
    stopped = true;
}

public void restart() {
    if (SpriteframeObject.size() == 0) {
        return;
    }
    stopped = false;
    currentFrame = 0;
}

public void reset() {
    this.stopped = true;
    this.frameCount = 0;
    this.currentFrame = 0;
}

private void addFrame(BufferedImage frame, int duration) {
    if (duration <= 0) {
        System.err.println("Invalid duration: " + duration);
        throw new RuntimeException("Invalid duration: " + duration);
    }

    SpriteframeObject.add(new SpriteFrame(frame, duration));
    currentFrame = 0;
}

public BufferedImage getSprite() {
    return SpriteframeObject.get(currentFrame).getFrame();
}

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

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

            if (currentFrame > totalFrames - 1) {
                currentFrame = 0;

               //if not looping for ever than stop

                if(!loop) 
                {
                    stopped = true;
                }
            }

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

    }

}
}

right now what is happing is that it only prints 1st image than stops. i want to do one full ciyle than stop. any ideas?

Ill give you a example:

for (int i=0;i<99;i++)
{
    if (i>0)
    {
        break;
    }
    else
    {
    //do whatever you want
    }
}

This makes it loop only once (for whatever reason you want). Do note, that this may or may not apply to your situation.

commented: It looks like you didn't even try to understand the question. Also, that code is an example of something nobody should ever write--everything outside the "else" block is meaningless and wasteful. -2

Are you sure it's stopping with the first frame, or is it possible that it's looping all the way around once and stopping on the first frame? That's what your code looks like it's doing. It will always reset currentFrame to zero, even if we're not looping.

Try changing this:

if (currentFrame > totalFrames - 1) {
    currentFrame = 0;
    //if not looping for ever than stop
    if(!loop) 
    {
        stopped = true;
    }
}

...to this:

if (currentFrame > totalFrames - 1)
{
    if(loop)
    {
        currentFrame = 0;
    }
    else // if not looping forever then stop
    {
        stopped = true;
        currentFrame -= animationDirection; // undo the movement
    }
}

...just to see what happens. This isn't the best way to handle not-looping, but it's the smallest change to the code that would help you check that behavior with a "forward" animation.

Better would be to rewrite the inner portion update to check currentFrame before adjusting it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.