javascript - Phaser 3: Starting a scene (stopping all other running scenes) from the global "game" object - St

As the title says.. Is there a way to stop all running scenes when starting a scene from the "game

As the title says.. Is there a way to stop all running scenes when starting a scene from the "game" object?

For now, when I call game.scene.start(...) from outside a Scene object, nothing happens. My current solution is to keep a global variable pointing to the latest started Scene (In all my scenes, I reset that global variable in the create() function with something like currentScene = this;) So when I want to start a new Scene I call currentScene.scene.start(...). But obviously that's not reliable because I could forget to reset my global variable (currentScene) in all my scenes, etc...

EDIT:

To clarifiy what I'm trying to acheive: I'm making a small multiplayer game with a couple scenes and a few GameObjects. I've a "socket" module that handles all traffic between the client and the server (I'm using socket.io). I would want to do something like:

this.socket.on('game_end', (data) => {
    // Server decides when the game stops and sends an event to all clients.
    // Here I need to stop the currently running scene (i.e. SceneInGame)
    // and show the "game finished / results" scene.
    this.game.scene.start('SceneGameEnd');
    // ^ That doesn't work. SceneInGame keeps playing and SceneGameEnd doesn't start
});

As the title says.. Is there a way to stop all running scenes when starting a scene from the "game" object?

For now, when I call game.scene.start(...) from outside a Scene object, nothing happens. My current solution is to keep a global variable pointing to the latest started Scene (In all my scenes, I reset that global variable in the create() function with something like currentScene = this;) So when I want to start a new Scene I call currentScene.scene.start(...). But obviously that's not reliable because I could forget to reset my global variable (currentScene) in all my scenes, etc...

EDIT:

To clarifiy what I'm trying to acheive: I'm making a small multiplayer game with a couple scenes and a few GameObjects. I've a "socket" module that handles all traffic between the client and the server (I'm using socket.io). I would want to do something like:

this.socket.on('game_end', (data) => {
    // Server decides when the game stops and sends an event to all clients.
    // Here I need to stop the currently running scene (i.e. SceneInGame)
    // and show the "game finished / results" scene.
    this.game.scene.start('SceneGameEnd');
    // ^ That doesn't work. SceneInGame keeps playing and SceneGameEnd doesn't start
});
Share Improve this question edited Feb 4, 2022 at 7:23 winner_joiner 15k4 gold badges39 silver badges70 bronze badges asked Sep 24, 2020 at 14:30 Jonathan AshbyJonathan Ashby 11 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You really shouldn't use game.scene at all in Phaser 3 (indeed, this won't even work as of Phaser 3.50.0). Scene operations should go via the ScenePlugin, which is accessible as this.scene from within any Scene. I.e. this.scene.start('otherScene') will stop the current Scene, then start the given one, in order, at the next game step. Where-as game.scene.start just blasts on through with no care to the game step or what's calling it.

There is no way to 'stop all running scenes' however, but then unless you have started multiple Scenes (via launch or a similar method), then there won't actually be any other running scenes. Show some actual code, then we can give a more targeted response.

There's a way I was able to do this, that at least works with Phaser 3.55 by accessing the current scene manually and starting the new scene from there.

For example: game.scene.keys.myCurrentScene.scene.start('SceneGameEnd') This will effectively call the this.scene of myCurrentScene and replace that scene by starting SceneGameEnd

Where myCurrentScene would be the key of the current running scene. You can see the scene key at game.scene.keys property where all scenes are listed by key name. However, this would only work if you know what's the key of the current running scene. Probably there's a way to get the current running scene but I can't answer that one.

There is however a more of a brute force workaround if you don't know the key of the current running scene. You could run a loop through all scenes, regardless if it is running or not, from the array of game.scene.scenes and call scene.stop on all scenes and then run just the one you need.

For example:

// Stop all scenes
game.scenes.forEach((scene) => {
  const key = scene.scene.key; // This is not a typo, the scene here is more like a "game" object, so the scene actually is under the "scene" property.
  game.scene.stop(key);
})
// After stopping all scenes, then you can start only the scene you need
game.scene.start('SceneGameEnd');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745116220a4612142.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信