phaser icon indicating copy to clipboard operation
phaser copied to clipboard

Destroying moving body causes exception and breaks the game

Open TreantBG opened this issue 3 years ago • 3 comments

Version

  • Phaser Version: Phaser v3.60.0-beta.17
  • Operating system: Windows 10 image

Description:

While player is moving i initiate destroy on the object or scene then this error happens I have put checks in place but as it is async codebase this error comes often

It's a simple fix to add if statement.

TreantBG avatar Jul 05 '23 21:07 TreantBG

Why are you using Beta 17? (or was that just a mistake when opening the issue)

Also, I need to see some code for this. Simply destroying a Body isn't enough to trigger this error. There must be something else going on.

photonstorm avatar Jul 06 '23 20:07 photonstorm

Sorry for the late response i fixed the issue

I changed to the latest version as well and it was happening again. After that i realized that maybe im using the destruction of the body wrong. You can see the commented line. That was causing the error (but not aways for some reason)

if (this.physicsBody) {
          this.scene.matter.world.remove(this.physicsBody)
          // this.physicsBody?.body?.gameObject?.destroy()
            this.physicsBody = undefined
      }

You can close this issue if you think it's ok

Edit: this.physicsBody is

this.physicsBody = this.scene.matter.add.image(this.x, this.y, "empty", undefined, {
           mass: 100,
           label: "character"
       })

       this.physicsBody.setBody({
           type: 'circle',
           radius: this.bodyRadius || 32
       })

TreantBG avatar Jul 17 '23 23:07 TreantBG

Thanks for updating your issue👍

photonstorm avatar Jul 21 '23 16:07 photonstorm