# Destroy is not working well

**URL:** <https://phaser.discourse.group/t/destroy-is-not-working-well/6121>\
**Category:** Phaser 3\
**Created:** [April 28, 2020, 11:47pm UTC](https://phaser.discourse.group/t/destroy-is-not-working-well/6121 "2020-04-28T23:47:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tacila](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/tacila/32/3637_2.png) [@Tacila](https://phaser.discourse.group/u/Tacila)\
**Post date:** [April 28, 2020, 11:47pm UTC](https://phaser.discourse.group/t/destroy-is-not-working-well/6121/1 "2020-04-28T23:47:50Z")

</div>

I’m making a game that when clicking on the sprite, life goes down to 0 then destroy. But destrou not work the sprite is active = false but still visible.  
I have class BichoGrafico and i recive the group of bichos :

class BichoGrafico extends Phaser.GameObjects.Sprite {

```
vida;

velocidade;

danoMartelo;

chave;

grupoBichos;

constructor(scene, x, y, texture, vida, velocidade, danoMartelo, grupoBichos) {

    super(scene, x, y, texture);

    this.chave = texture;

    scene.add.existing(this);

    scene.physics.add.existing(this);

    this.vida = vida;

    this.velocidade = velocidade;

    this.danoMartelo = danoMartelo;

    this.grupoBichos = grupoBichos;

    if (texture == 'bicho3') {

        let escala = window.devicePixelRatio / 7;

        this.setScale(escala);

    } else {

        let escala = window.devicePixelRatio / 9;

        this.setScale(escala);

    }

    this.setInteractive();

    this.on('pointerdown', this.morrer, this);

}

playHit() {

    var chaveSom = 'hit' + this.chave;

    this.scene.sound.add(chaveSom).play();

}

morrer() {

    this.vida -= this.danoMartelo;

    this.tint = 0xff0000;

    this.alpha -= .01;

    this.grupoBichos.clear();

    if (this.vida <= 0) {

        console.log('como está a vida ', this.vida, ' ql foi o dano ', this.danoMartelo);

        console.log('bicho ', this);

        this.playHit();

        // this.active = false;

        // this.visible = false;

        this.grupoBichos.remove(this, true ,true);

        this.destroy(this,true); // not working well

        this.grupoBichos.clear();

        console.log('bicho morreu');

    }

}

```

}

here wher this.gropoBichos is create in another class:

desenharBichosCreate() {

```
    this.groupBichos = this.scena.physics.add.group({

        classType: BichoGrafico,

        bounceX: 1,

        bounceY: 1,

        colliderWorldBoundes: true

    });

    this.timeEventB = this.scena.time.addEvent({ delay: 1000, callback: this.desenharBicho, callbackScope: this, loop: true })

}

desenharBicho() {

    if (this.gameOver == false && this.passouDeFase == false) {

        let buracoAlvo = this.buracosAtivos[UtilC.getNumEntre(0, this.buracosAtivos.length - 1)];

        for (let i = 0; i < buracoAlvo.qtdBichoPorBuraco; i++) {

            let chave = buracoAlvo.bichosPermitidos[UtilC.getNumEntre(0, buracoAlvo.bichosPermitidos.length - 1)];

            let bicho = this.getBichoPorChave(chave);

            if (bicho) {

                let bg = new BichoGrafico(this.scena, buracoAlvo.x, buracoAlvo.y,

                    bicho.chave, bicho.vida, bicho.velocidade, this.martelo.getMarteloHabilitado().dano, this.groupBichos);

                bg.angle = -90;

                this.groupBichos.add(bg);

                bicho.bichoGrafico = bg;

            }

        }

    } else {

        // this.timerEvents.remove(); 

        this.timeEventB.remove();

    }

}

```

please help me I upgrade phaser version for 3.23

---

<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:** [April 29, 2020, 12:51am UTC](https://phaser.discourse.group/t/destroy-is-not-working-well/6121/2 "2020-04-29T00:51:10Z")

</div>

```javascript
class BichoGrafico extends Phaser.GameObjects.Sprite {
  // …
  morrer() {
    this.vida -= this.danoMartelo;
    this.tint = 0xff0000;
    this.alpha -= 0.01;

    if (this.vida <= 0) {
      console.log('como está a vida ', this.vida, ' ql foi o dano ', this.danoMartelo);
      console.log('bicho ', this);

      this.destroy();

      console.log('bicho morreu');
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Tacila](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/tacila/32/3637_2.png) [@Tacila](https://phaser.discourse.group/u/Tacila)\
**Post date:** [April 29, 2020, 1:39am UTC](https://phaser.discourse.group/t/destroy-is-not-working-well/6121/3 "2020-04-29T01:39:57Z")

</div>

I try, and wrong continue I already try:  
This.destroy() // not working well  
This.destroy(true) // not working well  
This.destroy(this,true) // not work

---

<div class="post-metadata">

**Author:** ![Tacila](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/tacila/32/3637_2.png) [@Tacila](https://phaser.discourse.group/u/Tacila)\
**Post date:** [May 2, 2020, 2:22pm UTC](https://phaser.discourse.group/t/destroy-is-not-working-well/6121/4 "2020-05-02T14:22:04Z")

</div>

Thank’s! but you have another way? this didn’t work!

---

<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:** [May 2, 2020, 3:09pm UTC](https://phaser.discourse.group/t/destroy-is-not-working-well/6121/5 "2020-05-02T15:09:49Z")

</div>

I don’t know what’s not working. Can you make an example?

---

<div class="post-metadata">

**Author:** ![Tacila](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/tacila/32/3637_2.png) [@Tacila](https://phaser.discourse.group/u/Tacila)\
**Post date:** [June 26, 2020, 5:19pm UTC](https://phaser.discourse.group/t/destroy-is-not-working-well/6121/6 "2020-06-26T17:19:15Z")

</div>

I solve!  
The error was occurring because of the  
//for (let i = 0; i \< buracoAlvo.qtdBichoPorBuraco; i++) {

```
        let chave = buracoAlvo.bichosPermitidos[UtilC.getNumEntre(0, buracoAlvo.bichosPermitidos.length - 1)];

        let bicho = this.getBichoPorChave(chave);

        if (bicho) {

            let bg = new BichoGrafico(this.scena, buracoAlvo.x, buracoAlvo.y,

                bicho.chave, bicho.vida, bicho.velocidade, this.martelo.getMarteloHabilitado().dano, this.groupBichos);

            bg.angle = -90;

            this.groupBichos.add(bg);

            bicho.bichoGrafico = bg;

     // }

```

remove the “for” and it will work
