problem with sprite animation

I’m making a simple space shooter game and I’m still fighting with timing stuff…

Now, if I have for example 60 aliens to kill on the screen the animation of each one is fine, but when I have less then 10 for example, the animation is very slow (The change of the frames of the animation takes more time to change).

Why this is happing?

Post code.

Kev

There it is:

main loop


      public void gameLoop() {
            long lastTicks, currentTicks, elapsedMs, ticksPerSecond;
            AdvancedTimer timer = new AdvancedTimer();
            ticksPerSecond = AdvancedTimer.getTicksPerSecond();
            long sleepTime = AdvancedTimer.getTicksPerSecond() / FPS;
            long ticks = 0;
            timer.start();
            lastTicks = timer.getClockTicks();
            while (isRunning) {
                  
                  currentTicks = timer.getClockTicks();
                  elapsedMs = ((currentTicks - lastTicks) * 1000) / ticksPerSecond;

                  fps++;
                  lastFpsTime += elapsedMs;

                  // Update fps counter if 1 sec has passed.
                  if(lastFpsTime >= 1000) {
                        this.setTitle("FPS=" + String.valueOf(fps));
                        fps=0;
                        lastFpsTime =0 ;
                  }
                  
                  
            Graphics g = this.strategy.getDrawGraphics();
            g.setColor(Color.black);
            g.fillRect(0, 0, 800, 600);

                  // move/update anim and draw the sprites
            for (int i = 0; i < this.sprites.size(); i++) {
                Sprite sprite = (Sprite) sprites.get(i);
                sprite.move(elapsedMs);
                sprite.draw(g);
            }
                  
                  
                  
                  lastTicks = currentTicks;
                  timer.sleepUntil(ticks + sleepTime);
                  ticks += sleepTime;
            }
            timer.stop();
            System.exit(0);
      }

move method of class Alien that extends Sprite

      
      public void move(long pDelta) {
        this.animation.animate(pDelta);

        if ((dx < 0) && (x < 10)) {
            this.game.updateLogic();
        }

        if ((dx > 0) && (x > 750)) {
            this.game.updateLogic();
        }
            
            // call move on Sprite class
        super.move(pDelta);
    }

This is the code of the move method on Sprite:

      
      // dx is about 100ms
      public void move(long pDelta) {
        this.x += ((pDelta * dx) / 1000);
        this.y += ((pDelta * dy) / 1000);
    }

And this is the animate method of the Animation class. Here that the next frame
of the sprite is determined.


      // each frame takes 250ms(frameDuration)
    public void animate(long pDelta) {
        this.increaseLastFrameChangeBy(pDelta);

        if (this.getLastFrameChange() > this.getFrameDuration()) {
            this.setLastFrameChange(0);
            this.increaseFrameNumber();

            if (this.getFrameNumber() >= this.getTotalFrames()) {
                this.setFrameNumber(0);
            }
        }
    }
    
    public void increaseLastFrameChangeBy(long pDelta) {
        this.lastFrameChange += pDelta;
    }

    public void increaseFrameNumber() {
        this.frameNumber++;
    }
    
    public void draw(Graphics g, int pX, int pY) {
        g.drawImage(this.frames[this.frameNumber], pX, pY, null);
    }
    public void setFrameDuration(long frameDuration) {
        this.frameDuration = frameDuration;
    }
    
    

I did not understand why the speed of the animation changes when I have different number of sprites on the screen. For example over 50 sprites the animation is fast and below 10 the animation is slow…

any tips about this? I could not solve it yet

is the animation object you are using shared or is there one for each sprite?

it’s shared. I have a repositry where I load all the animations. Then I create the sprites using this previous loaded animations.

do u think that the shared animation is the problem? I have posted another thread on this subject (with full source) called “animation problem” here in 2D forum

ok, I think that I have solved my problem! 8) The problem was that I was sharing the animations will all sprites. The solution was very simple, I have just created a new method in Animation class for cloning the object. I don’t know if it was the best solution for this kind of situation but it is working much better now.

Thank you JuddMan for the light!!! ;D