Phaser3 Scenes transitions

自闭症网瘾萝莉.ら 提交于 2020-01-14 10:13:52

问题


I'm new to Phaser3 and before starting a crazy project, I want to know how I should start, switch between scenes. I saw that there are several functions, start, launch, switch, run, resume, pause, etc...

Example, lets say I want to have 2 scenes, a Menu and a Game. I boot on the Menu and I want to go to the Game scene and if I click on a button then come back to the Menu scene. I've achieved this by calling the start function, but I noticed that the all, init, preload and create functions are called everytime and therefore I'm loading all the images, setting all the listener over and over again.

This seems wrong, should I be using the launch or switch functions and pausing and resuming? But how do I hide the previous scene?

Thanks in advance.


回答1:


This question might be a little too broad, but with Phaser 3 in mind, it still depends upon what purpose your menu serves.

I think most games have a main menu that will generally be called when the game first starts, and then won't be called again.

If this is an in-game menu, where settings can be changed or part of the game can be reset/restarted, then it might not make sense to redirect to a completely different scene.

With Phaser 3's support of multiple scenes - with Dev Log #119 and Dev Log #121 probably being the best current sources of information - another option would be to start a new scene within the current scene to handle this.

However, if this is really just UI, there's nothing to stop you from creating an overlay, instead of spawning an entire scene.

If you're concerned about performance I might think about whether the entire menu needs to be called, or if a simplified menu would work. Also, make sure that you're preloading assets before you're in the menu and main game.

I personally use Boot > Preloader > Splash Screen > Main Menu > Main Game scenes, where the Preloader loads the majority of the assets I'll need. This has the downside of a longer initial load, but the upside of minimal loading after this point.

Scene Transitions

How I handle these in my starter templates is to add the scenes to the Scene Manager when creating the scene. Then I transition by start to the first scene.

this.scene.add(Boot.Name, Boot);
this.scene.add(Preloader.Name, Preloader);
this.scene.add(SplashScreen.Name, SplashScreen);
this.scene.add(MainMenu.Name, MainMenu);
this.scene.start(Boot.Name);

Then I simply keep starting the next scenes as needed.

this.scene.start(Preloader.Name);

For another game that uses multiple scenes I ended up creating the following function (TypeScript) to handle this:

private sleepPreviousParallelScene(sceneToStart: string): Phaser.Scene {
    if (this.uiSceneRunning !== sceneToStart) {
        // Make sure that we properly handle the initial state, when no scene is set as running yet.
        if (this.uiSceneRunning !== "") {
            this.scene.get(this.uiSceneRunning).scene.sleep();
        }
        const newScene = this.scene.get(sceneToStart);
        newScene.scene.start();
        this.scene.bringToTop(sceneToStart);
        this.uiSceneRunning = sceneToStart;

        return newScene;
    } else {
        return this.scene.get(this.uiSceneRunning);
    }
}

In the game I was using this for, I was trying to replicate a standard tab interface (like what's see in the Dev Logs above with the file folder-like interface).




回答2:


Ok, here is the deal. In phaser 3, the start function actually SHUTS DOWN the previous scene as it starts the new one. This is why you see the init, preload ext every time. If however, you 'launch' your scenes instead, then they will not go through the whole shut down, restart sequence. You can move them to the top or bottom, set to receive input or not, etc. HOWEVER, BEWARE! As of phaser 3.16, sleep or pause of a scene does NOT shut down the update. As a result, if you launch a scene, then sleep it, it basically does nothing other than maybe set the flag, saying it's asleep even though it really isn't. So any update processing will continue in any launched scene. You can short circuit the update with a flag of some sort at the beginning, (that's what I've resorted to), but the other gotcha is the camera size. All the scenes must therefore have the same camera size or they will render (even if "sleeping") and mess up your display. So, bottom line, it's going to be messy until sleep and pause actually work.



来源:https://stackoverflow.com/questions/53238869/phaser3-scenes-transitions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!