How to Spawn Enemies in Waves?

I’ve been struggling with this for a while but how can I spawn groups of enemies in waves?

I initially spawn enemies using this code inside my Game scene’s create() method:

//Initial spawn of enemies for wave 1
enemyGrunts = this.physics.add.group({
    key: 'enemyGrunt',
    repeat: gruntAmount - 1,
    setXY: { x: 60, y: 0, stepX: 60 }
});
enemyGrunts.children.iterate(function(child){
    child.setVelocity(0, Phaser.Math.FloatBetween(gruntMinSpeed, gruntMaxSpeed));
});

I then try to spawn more inside of my update() method when the player has “killed” all the grunts in the current wave:

update(){
if (gruntAmount === 0){
        if(currentWave === maxWave){ //If we're on wave 3 (max) and gruntAmount is zero, that means the player has defeated all waves
            this.victoryText.visible = true;
            gameOver = true; 
        }
        else{
        currentWave += 1; //Increment the wave amount to make more baddies spawn
        gruntAmount = currentWave * 2
        enemyGrunts.createMultiple({
            repeat: gruntAmount - 1,
            setXY: { x: 60, y: 0, stepX: 60 } 
        })
        enemyGrunts.children.iterate(function(child){
            child.setVelocity(0, Phaser.Math.FloatBetween(gruntMinSpeed, gruntMaxSpeed));
        });
      }
    }
}

I also have some colliders that uses the enemyGrunts group to check for collisions with the players bullets and the some other stuff:

//Disable the enemy grunts when hit with a antibody
this.physics.add.collider(enemyGrunts, playerBullets, enemyHitCallback, null, this);
//Completely remove the grunts by calling remove() on the child if they touch the player after being antibodied. Also remove them from collision checks.
this.physics.add.overlap(player, enemyGrunts, collectGruntCallback, null, this);
//Disable the grunts when they touch the lungs, then tint the lungs
this.physics.add.collider(enemyGrunts, lungsBG, lungsHitCallback, null, this);

My problem is that the enemyGrunts.createMultiple method isn’t doing anything and I can’t see the newly spawned grunts is it is doing anything at all. What am I doing wrong?

createMultiple() needs { key } to create anything.

1 Like

Wow that was an easy fix. Thanks so much.

Hi. :slight_smile:
can you share the logic of gruntAmount?
when should it be 0 again?