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

GameObjects added to Layers are destroyed twice on Scene shutdown causing crashes #6675

Closed
crockergd opened this issue Nov 21, 2023 · 4 comments

Comments

@crockergd
Copy link

Version

  • Phaser Version: 3.70.0
  • Operating system: Windows 10
  • Browser:

Description

Adding a GameObject to a Layer throws unhandled exceptions when the Scene they're added to shuts down (such as when transitioning to a new scene).

The Scene destroys the Layer, then the Layer destroys the Sprite which causes it to emit its DESTROY event. This event is caught by the Layer which removes the Sprite from its list as well as attempts to destroy it again because the Layer is being destroyed by the scene. The destroy attempted by the Layer throws an exception in Sprite.preDestroy when this.anims is null because it was already removed by the original destroy.

Example Test Code

https://codepen.io/crockergd/pen/RwvMxNG

The demo attempts to add a Sprite to a Layer, then transition to a different Scene which displays a small green square. Currently, the demo crashes while trying to stop the current scene. The demo completes successfully if the line adding SceneA's rect to the layer is removed, or if the Phaser dependency is set to a version before 3.70.0.

Additional Information

The exception thrown by the demo doesn't trace the source of the issue well, I've attached an image of a call stack from a debugger as well.

layercrashstack

@zommerfelds
Copy link

I'm having the same issue. But for me it only happens when I restart the scene (this.scene.restart()). If I switch to a new scene this is not a problem.

@zommerfelds
Copy link

In case it helps anyone, my workaround is to run something like this before restarting the scene:

        for (let child of this.children.getAll()) {
            if (child instanceof Phaser.GameObjects.Layer) {
                child.destroy();
            }
        }

@valuks
Copy link

valuks commented Jan 2, 2024

My example with an error and temporary fix (check the comments): https://codepen.io/valentoros/pen/VwRvGqW

@photonstorm
Copy link
Collaborator

Thank you for submitting this issue. We have fixed this and the fix has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants