Jump to content

Why does my game crash occasionally when switching scenes?


Goshawk
 Share

Recommended Posts

My game is crashing infrequently.

I have three scenes, `BootGame`, `PlayGame` and `Menu`. I switch scenes as follows:

play_game(){
    this.scene.start('PlayGame');
}

The game goes back and forth between `PlayGame` and `Menu` but every now and again (about 1 in 50 times), the game crashes when switching scenes.

The error in the console is:

Uncaught TypeError: Cannot read property 'update' of null
    at ArcadePhysics.shutdown (project.bundle.js:148885)
    at EventEmitter.emit (project.bundle.js:2506)
    at Systems.shutdown (project.bundle.js:35059)
    at SceneManager.stop (project.bundle.js:68428)
    at SceneManager.processQueue (project.bundle.js:67563)
    at SceneManager.update (project.bundle.js:67822)
    at Game.step (project.bundle.js:119399)
    at TimeStep.step (project.bundle.js:122906)
    at step (project.bundle.js:57645)

This happens so infrequently, I have no idea what's causing it at the moment other than it has something to do with changing scenes.

Is there anything that I'm obviously doing wrong? Am I changing scenes correctly? Are some extra steps I need to take when changing scenes?

Link to comment
Share on other sites

Not sure if it's connected but memory usage increases when switching scenes.

Which leads me to suspect that I'm not switching scenes correctly as I'm creating new objects (sprites, audio, etc...) each time I start the game scene. I either need to make sure these are all destroyed when starting the menu scene or look at the examples to see where I'm going wrong.

Any suggestions?

Screen Shot 2018-07-28 at 14.56.40.png

Link to comment
Share on other sites

I'm using the latest release.

The reason I'm concerned about the memory usage is that both the cumulative increase in memory usage and the crashes occur when switching scenes. This leads me to believe I'm doing something wrong/not doing something I'm supposed to do.

I'll blame the wine for leading me to misinterpret the memory usage timeline. Interestingly, I'm now using CANVAS instead of AUTO and the game has yet to crash.

Link to comment
Share on other sites

Still unsure what's happening.

The topmost error message points to this line:

    /**
     * The Scene that owns this plugin is shutting down.
     * We need to kill and reset all internal properties as well as stop listening to Scene events.
     *
     * @method Phaser.Physics.Arcade.ArcadePhysics#shutdown
     * @since 3.0.0
     */
    shutdown: function ()
    {
        var eventEmitter = this.systems.events;

        eventEmitter.off('update', this.world.update, this.world); // <- this line

 

Link to comment
Share on other sites

I'm now setting a flag to true when I want to switch to the menu scene. The actual `this.scene.start('Menu')` call is now done at the end of `update()`. This seems to have fixed it. I'm sure I'll have a better understanding of what's going on when I become more familiar with the framework.

Link to comment
Share on other sites

As above. I set a flag when I want to end the game and I switch the scenes in `update()`

update(){
    ...

    if(this.gameEnded){
        this.scene.start('Menu');
    }
}

The presumption I've made is that putting it at the end of `update()` ensures that Phaser has done all the updating it needs to do before the scene is destroyed. I have a handful of timeouts and time events in the scene and I wonder if they were causing the problems -- essentially a race condition where the scene was destroyed before updates could be called on it. 

I haven't done any testing apart from playing the game but it hasn't crashed yet. Have you any timeouts or time events in your scene?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...