'update' not working after restart a scene

Hi I’ve tried to reload a scene after remove/destroy it, but the update lifecycle method does NOT run again.

Here is my codes:

/** util.ts **/

export let pGame: Game | undefined
const initSceneName = 'init'
let curSceneName = initSceneName

class InitScene extends Scene {
}
pGame = new Game({
    ...phaserBaseConfig,
    scene: [],
})
pGame.scene.add(initSceneName, InitScene, true)

export const resetScene = (
  sceneName: string,
  scene: Phaser.Scene
) => {
  let newScene = pGame.scene.getScene(sceneName)?.scene

  if (!newScene) {
    pGame.scene.add(sceneName, scene)
  }

  pGame.scene.stop(curSceneName)
  pGame.scene.start(sceneName)

  curSceneName = sceneName
}

export const destroyScene = () => {
  resetScene(initSceneName, InitScene)
}

These methods be used like this:

/** test-scene.ts **/
  import { destroyScene, resetScene } from 'util.ts'

  class TestScene extends Scene {
    preload() {
      console.log('preload calls...')
      // preload some textures and sounds...
    }

    create() {
      console.log('create calls...')
      // some animations here...
    }

    update() {
      console.log('update calls...')
      // ...
    }
  }

  function initTestScene() {
    resetScene(
      'testScene',
      TestScene
    )
  }

  initTestScene()  // runs well

  setTimeout(() => {
    destroyScene()
  }, 5000)

  setTimeout(() => {
    // 'preload' and 'create' will run (showing the logs by console.log, but no animation shows).
    // 'update' method of TestScene could not be triggered again.
    initTestScene()
  }, 8000)

IMO update should run again following preload and create methods after 8 secs, but this doesn’t happen (without error).

What’s more, none animation shows after 8 secs, althought the preload and create had been running.

This behavior confuses me so much, what can I do to fix it?

Thx!

Add logs for add, stop, start in resetScene.

1 Like

Sorry seems some methods of Phaser GameObject being rewrote inner TestScene, and I guess that the reason for this issue.

I will leave more details & solution here while I figure out this problem.

Plz just ignore this topic. :heart:

I’ve got the final reason: game.pause called by TestScene before swithing scenes.

The sollution is just resume the game while swithing scenes:

export const resetScene = (
  sceneName: string,
  scene: Phaser.Scene
) => {
  if (pGame.isPaused) {
    pGame.resume()  // resume game here!!!!!!!!!
  }

  // ...
}