Performance issue for spawning 200 enemies

Hello guys.
I’m trying to create multiple enemies and multiple projectiles and it’s giving me a lot of stuttering.
I’m creating my enemies before and after using the kill command, but the kill command is not working “e.kill is not a function”. So I have two problems. The first one I’m trying to recycle enemies with the code below.

create(){
       this.grupoInimigo = this.physics.add.group({
            allowGravity: false,
            immovable: true,
            setCollideWith: true,
            setCollisionGroup : true
        })
        this.grupoInimigo.estagio = 1
        
        this.SpawnInimigo = (nomeInimigo) =>{
            
            let op = Phaser.Math.Between(0, 3)
            if(op==0){
                let x = Phaser.Math.Between(this.jogador.x - 400, this.jogador.x + 400)
                let y = Phaser.Math.Between(this.jogador.y - 320,this.jogador.y -330)
                var inimigo = this.physics.add.sprite(x,y,nomeInimigo).anims.play('barataAnim')
                inimigo.HP=20*2*this.jogador.nivel
                inimigo.angle = -180
                inimigo.dano= 2*1.2*this.jogador.nivel
                inimigo.velocidade = 180
                inimigo.imune = 100
                inimigo.tempCausaDano = 0
                inimigo.colidiu = false
                inimigo.xp = 35*1.3*this.jogador.nivel
                
            }else if(op ==1 ){
                let x = Phaser.Math.Between(this.jogador.x - 400, this.jogador.x + 400)
                let y = Phaser.Math.Between(this.jogador.y + 320,this.jogador.y +330)
                var inimigo = this.physics.add.sprite(x,y,nomeInimigo).anims.play('barataAnim')
                inimigo.HP=20*2*this.jogador.nivel
                inimigo.dano=2*1.2*this.jogador.nivel
                inimigo.velocidade = 180
                inimigo.imune = 100
                inimigo.tempCausaDano = 0
                inimigo.colidiu = false
                inimigo.xp = 35*1.3*this.jogador.nivel

            }else if(op == 2 ){
                let x = Phaser.Math.Between(this.jogador.x - 420, this.jogador.x - 410)
                let y = Phaser.Math.Between(this.jogador.y + 300,this.jogador.y - 300)
                var inimigo = this.physics.add.sprite(x,y,nomeInimigo).anims.play('barataAnim')
                inimigo.HP=20*2*this.jogador.nivel
                inimigo.angle = 90
                inimigo.dano=2*1.2*this.jogador.nivel
                inimigo.velocidade = 180
                inimigo.imune = 100
                inimigo.tempCausaDano = 0
                inimigo.colidiu = false
                inimigo.xp = 35*1.3*this.jogador.nivel

            }else {
                let x = Phaser.Math.Between(this.jogador.x + 410, this.jogador.x + 420)
                let y = Phaser.Math.Between(this.jogador.y + 300,this.jogador.y - 300)
                var inimigo = this.physics.add.sprite(x,y,nomeInimigo).anims.play('barataAnim')
                inimigo.HP=20*2*this.jogador.nivel
                inimigo.dano=2*1.2*this.jogador.nivel
                inimigo.angle = -90
                inimigo.velocidade = 180
                inimigo.imune = 100
                inimigo.tempCausaDano = 0
                inimigo.colidiu = false
                inimigo.xp = 35*1.3*this.jogador.nivel

            }
           
            this.grupoInimigo.add(inimigo)
            // da leg this.physics.add.collider(this.grupoInimigo);  
            this.nInimigos+=1
            if(this.nInimigos>=this.mInimigos)
            this.physics.add.collider(this.grupoInimigo);  
             
        }
        for(var i = 0;i<(this.levelMax*1.8+5);i++){
            this.SpawnInimigo('barataAnim')
        }
        this.limpaWave()
        this.SpawnInimigo.revive = () =>{
            var inimigo = this.grupoInimigo.getFirstExists(false)
            if(inimigo){
                inimigo.revive()
            }
        }
     
  limpaWave(){
        this.grupoInimigo.children.iterate((e) =>{
            e.kill();
        })
    }

Probably the revive() command will give an error too

Another thing I’m thinking about is turning off physics, to lessen lag, so would I have to check for collisions manually? And tell enemies to chase the player manually?
I know it’s a lot of questions, but I searched and didn’t find much. Who knows, this thread might help more people.
Thanks!!

I replied to a few threads and mine went down. just giving up

Sprites have no kill() or revive() method in Phaser 3. See

1 Like

Fits like a glove, thank you very much. I tested it with 500 enemies and it didn’t crash, now I’m going to put it in the projectiles. Which will help a lot.
I owe you one more :slight_smile: