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!