'Is there a way to check if an object is really released?

Per the doc, destroy() method

Destroys this Game Object removing it from the Display List and Update List and severing all ties to parent resources.

Also removes itself from the Input Manager and Physics Manager if previously enabled.

Use this to remove a Game Object from your game if you don't ever plan to use it again. As long as no reference to it exists within your own code it should become free for garbage collection by the browser.

If you just want to temporarily disable an object then look at using the Game Object Pool instead of destroying it, as destroyed objects cannot be resurrected.

I wrote this code to check if an object is really released

class BootScene extends Phaser.Scene {
  constructor() {
    super({ key: 'BootScene' });
  }
  create() {
    this.bullet = this.add.circle(50, 50, 10, 0xff0000);
    this.physics.add.existing(this.bullet);
    this.bullet.body.setVelocity(150, 0);

    this.slow_delta = 0;

    this.bullet.body.setCollideWorldBounds(true);
    this.bullet.body.onWorldBounds = true;
    this.bullet.key = 'enemy'
    this.bullet.body.world.on('worldbounds', () => {
      console.log('destroy')
      this.bullet.destroy();
    })
  }

  update(time, delta) {
    // examine the t value every 100 ms
    this.slow_delta += delta;
    if (this.slow_delta > 1000 && time < 100000) {
      this.slow_delta = 0;
      console.log(time, this.bullet.x);
    }
  }
}

var config = {
  width: 800,
  height: 500,
  physics: {
    default: 'arcade',
    arcade: {
      gravity: { y: 0 },
    }
  },
  scene: [BootScene]
}

var game = new Phaser.Game(config);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>

and found after bullet goes out of world, its position stays at 790.

However, update() can still get its position rather than undefined which seems to mean the object is not actually released.

Is there a way to check if an object is really released?

with @winner_joiner's reminder, I also tried this code

const cleanup = new FinalizationRegistry(key => {
});
this.bullet.body.world.on('worldbounds', () => {
  console.log('destroy')
  this.bullet.destroy();
  cleanup.register(this.bullet, 'werwer');
})

bullet still stays there.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source