Restarting scene from SceneManager

Hello! I am struggling to make a thing I thought will be very easy to implement. What am I trying to do is restarting my only scene on window resize event. That is a part of bigger case. Due to app architecture I don’t want to catch window-resize event from scene but want to catch it outside all scenes. I tried something like that

window.addEventListener('resize', function () {
  var scene = game.scene.getScene('Demo')

  scene.events.once('destroy', function() {
    game.scene.start('Demo');
  }
  game.scene.stop('Demo')
  // or game.scene.stop('Demo')
})

But that is not working for me. Latest phaser. Scene seems to stop but do not emit ‘destroy’ event so my play() function not being triggered.

It looks like I am missing something simple. Will be very grateful if someone will have some spare time to help me make this working. Thanks.

scene.stop() causes the shutdown event, not destroy.

I think all you need is

window.addEventListener('resize', function () {
  game.scene.getScene('Demo').scene.restart();
});

But window resize could fire more or less continuously, would that be a problem?

1 Like