Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error stopping a scene: cannot read property 'destroy' of undefined #5520

Closed
schontz opened this issue Jan 23, 2021 · 8 comments
Closed

Error stopping a scene: cannot read property 'destroy' of undefined #5520

schontz opened this issue Jan 23, 2021 · 8 comments

Comments

@schontz
Copy link
Contributor

schontz commented Jan 23, 2021

Version

  • Phaser Version: 3.5.1
  • Operating system: Mac and Windows
  • Browser: Chrome

Description

Shutting down a scene causes an error:

TypeError: Cannot read property 'destroy' of undefined
    at DisplayList.shutdown (phaser.js:163448)
    at EventEmitter.emit (phaser.js:1928)
    at Systems.shutdown (phaser.js:47907)
    at SceneManager.stop (phaser.js:99258)

The error in DisplayList is here:
https://github.com/photonstorm/phaser/blob/master/src/gameobjects/DisplayList.js#L234

I believe the issue is that some of the objects being destroyed are in turn destroying some sub-objects, which would be on this.list, thus making the list shorter.

This could be fixed like so:

while(list.length) {
  list[0].destroy(true);
}

That would be unaffected by changes to the list along the way.

@schontz schontz changed the title Error stopping a scene: cannot call destroy on undefined Error stopping a scene: cannot read property 'destroy' of undefined Jan 23, 2021
@schontz
Copy link
Contributor Author

schontz commented Feb 5, 2021

I did a little more digging and it seems my original hunch is right. I have something like the following:

class MySprite extends Phaser.Physics.Arcade.Sprite {
  constructor() {
    this.label = this.scene.add.text(10, 20, 'hello');
  }

  destroy() {
    this.label.destroy();
  }
}

When I trace DisplayList.shutdown I get something like so:

- shutdown: destroy (i = 7) = SomeObject
  - SomeObject.destroy
- shutdown: destroy (i = 6) = AnotherObject
  - AnotherObject.destroy
- shutdown: destroy (i = 5) = MySprite
  - MySprite.destroy
  - TextLabel.destroy
- shutdown: destroy (i = 4) = undefined because TextLabel was already destroyed.

Looking at the base GameObject.destroy, you can see that destroying removes the item from the display list, thus naively using i in shutdown is not a good option. I am currently getting around the issue by manually calling the code in my initial comment before shutdown runs.

@daneren2005
Copy link

I am running into the same issue and for the same reason. It is really helpful to be able to let objects manage their own dependencies without needing special logic to handle scene destruction separately.

@pt-hieu
Copy link

pt-hieu commented Dec 19, 2021

any update on this? I've been having this problem and it's like I don't have any destroy or remove called in my code

schontz added a commit to schontz/phaser that referenced this issue Dec 22, 2021
schontz added a commit to schontz/phaser that referenced this issue Dec 22, 2021
photonstorm added a commit that referenced this issue Feb 28, 2022
destroy game objects during shutdown safely #5520
@photonstorm
Copy link
Collaborator

Thanks for opening this issue, and for submitting a PR to fix it. We have merged your PR into the master branch and attributed the work to you in the Change Log. If you need to tweak the code for whatever reason please submit a new PR.

@yaasinhamidi
Copy link

yaasinhamidi commented Apr 5, 2022

Hey guys

I got this problem and trace it that maybe my components have problem but related to image that used in components and those images earlier have been destroyed in destroyed components

I have been changed phaser-arcade-physics.js line 159195 (shutdown function) to this and solved this problem:

while (i--)
{
if(typeof list[i] !== "undefined") {
list[i].destroy(true);
}
}

those list item that have been destroyed earlier, typeof list[i] !== "undefined" can skip destroyed items by this code.

@fromnowhereuser
Copy link

Hello, i have this problem too, when i try to delete a phaser "container" object. I really dont know how to handle this. My scene is replicated. I am using phaser within an angular comp...
@photonstorm do you have any news ?

@evan-boissonnot
Copy link

Hey guys

I got this problem and trace it that maybe my components have problem but related to image that used in components and those images earlier have been destroyed in destroyed components

I have been changed phaser-arcade-physics.js line 159195 (shutdown function) to this and solved this problem:

while (i--) { if(typeof list[i] !== "undefined") { list[i].destroy(true); } }

those list item that have been destroyed earlier, typeof list[i] !== "undefined" can skip destroyed items by this code.

it's the solution i want to do ... but it's not good ...

@photonstorm
Copy link
Collaborator

it's the solution i want to do ... but it's not good ...

This is already fixed in the master branch. Please make a build from there and use that.

derrickadkins added a commit to derrickadkins/phaser-tanks that referenced this issue Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants