1
\$\begingroup\$

Hello everyone i have a question about animating multiple enemies on the screen at the same time.

Basically, i figured out how to use the Pool for creating enemies and dispose them after use etc.. All enemies are equal to each other, and all enemies have 3 different animations for creating, moving, destroying.

Everything is working fine, but when i have multiple enemies on screen, they are all animating at the same times, even if they were (obviously) created in different times.

For example, i create 3 enemies, make them moving on the screen and they all have the same frame.. the animation must be the same for all, but i expected that the time when they change frame should be different.

The animations are instantiated inside the Enemy class, so i guess every instance of Enemy must have its own animation.

This is how i draw them in the Renderer class:

private void drawEnemies(float runTime) {
    enemyArrayLen = catta.activeEnemies.size;
    for (int i = enemyArrayLen; --i >= 0;) {
        currentEnemyAnimation = catta.activeEnemies.get(i).getCurrentAnimation();
        batcher.draw(
            currentEnemyAnimation.getKeyFrame(runTime),
            catta.activeEnemies.get(i).getX(),
            catta.activeEnemies.get(i).getY(),
            catta.activeEnemies.get(i).getWidth() / 2.0f,
            catta.activeEnemies.get(i).getHeight() / 2.0f,
            catta.activeEnemies.get(i).getWidth(),
            catta.activeEnemies.get(i).getHeight(),
            1,
            1,
            0
        );
    }
}

Enemies are stored in the catta.activeEnemies array.

currentEnemyAnimation is a "support" variable in which i put the Enemy in every cicle of the for cicle.

This is how i initialize an Enemy:

public void init(float posX, float posY, int width, int height) {
    this.position.set(posX,  posY);
    this.width = width;
    this.height = height;
    this.alive = true;
    this.enemyRunTime = 0;
    this.creationTime = 0.8f;
    this.destructionTime = 0.8f;
    this.crea_Animation = AssetLoader.enemy_creaAnimation;
    this.crea_Animation.setFrameDuration(this.creationTime / 4);
    this.mo_Animation = AssetLoader.enemy_moAnimation;
    this.des_Animation = AssetLoader.enemy_desAnimation;
    this.des_Animation.setFrameDuration(this.destructionTime / 4);
    this.enemyState = EnemyState.CREATING;
    ...

Maybe it's all a runTime and delta matter?

\$\endgroup\$
1
\$\begingroup\$

SOLVED, i had to pass the enemyRunTime to batcher when drawing each enemy, instead of the global runTime.. like this:

private void drawEnemies(float runTime) {
    enemyArrayLen = catta.activeEnemies.size;
    for (int i = enemyArrayLen; --i >= 0;) {
        currentEnemyAnimation = catta.activeEnemies.get(i).getCurrentAnimation();
        batcher.draw(
            currentEnemyAnimation.getKeyFrame(catta.activeEnemies.get(i).getEnemyRunTime()),
            catta.activeEnemies.get(i).getX(),
            catta.activeEnemies.get(i).getY(),
            catta.activeEnemies.get(i).getWidth() / 2.0f,
            catta.activeEnemies.get(i).getHeight() / 2.0f,
            catta.activeEnemies.get(i).getWidth(),
            catta.activeEnemies.get(i).getHeight(),
            1,
            1,
            0
        );
    }
}

Now each enemy animates independently from the others

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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