# Performance issue for spawning 200 enemies

**URL:** <https://phaser.discourse.group/t/performance-issue-for-spawning-200-enemies/12128>\
**Category:** Phaser 3\
**Created:** [August 6, 2022, 5:55pm UTC](https://phaser.discourse.group/t/performance-issue-for-spawning-200-enemies/12128 "2022-08-06T17:55:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiagobeta](https://avatars.discourse-cdn.com/v4/letter/t/e47774/32.png) [@thiagobeta](https://phaser.discourse.group/u/thiagobeta)\
**Post date:** [August 6, 2022, 5:55pm UTC](https://phaser.discourse.group/t/performance-issue-for-spawning-200-enemies/12128/1 "2022-08-06T17:55:00Z")

</div>

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.

```javascript
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!!

---

<div class="post-metadata">

**Author:** ![thiagobeta](https://avatars.discourse-cdn.com/v4/letter/t/e47774/32.png) [@thiagobeta](https://phaser.discourse.group/u/thiagobeta)\
**Post date:** [August 6, 2022, 6:32pm UTC](https://phaser.discourse.group/t/performance-issue-for-spawning-200-enemies/12128/2 "2022-08-06T18:32:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [August 7, 2022, 3:43pm UTC](https://phaser.discourse.group/t/performance-issue-for-spawning-200-enemies/12128/3 "2022-08-07T15:43:33Z")

</div>

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

> [@Arcade Sprite Pool](https://phaser.discourse.group/t/arcade-sprite-pool/1209):
>
> When [pooling ordinary sprites](https://labs.phaser.io/edit.html?src=src%5Cgame%20objects%5Cgroup%5Csprite%20pool.js) you toggle a sprite’s [active](https://newdocs.phaser.io/docs/3.80.0/Phaser.Physics.Arcade.Sprite#active) and [visible](https://newdocs.phaser.io/docs/3.80.0/Phaser.Physics.Arcade.Sprite#visible) properties on when the sprite is in use and off when it’s out of use (free). With physics sprites you’ll also toggle the [body.enable](https://newdocs.phaser.io/docs/3.80.0/Phaser.Physics.Arcade.Body#enable) property on and off at the same time. It’s convenient to use the [enableBody()](https://newdocs.phaser.io/docs/3.80.0/Phaser.Physics.Arcade.Sprite#enableBody) and [disableBody()](https://newdocs.phaser.io/docs/3.80.0/Phaser.Physics.Arcade.Sprite#disableBody) methods because they can set all 3 properties at once. When recycling you should also use the reset, x, and y arguments in enableBody() to synchronize the body and game object at a new position: g…

> [@Bullet pools (Phaser 3)](https://phaser.discourse.group/t/bullet-pools-phaser-3/7502):
>
> [Space battle 480x480] A bullet pool is a [sprite pool](https://phaser.discourse.group/t/arcade-sprite-pool/1209) where all the sprites are initially inactive. They are activated when fired and then deactivated when they collide or expire. Bullet pools Bullet pools with extended game object class More examples

---

<div class="post-metadata">

**Author:** ![thiagobeta](https://avatars.discourse-cdn.com/v4/letter/t/e47774/32.png) [@thiagobeta](https://phaser.discourse.group/u/thiagobeta)\
**Post date:** [August 8, 2022, 8:03pm UTC](https://phaser.discourse.group/t/performance-issue-for-spawning-200-enemies/12128/4 "2022-08-08T20:03:50Z")

</div>

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 🙂
