Phaser3场景过渡 [英] Phaser3 Scenes transitions

查看:677
本文介绍了Phaser3场景过渡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Phaser3 的新手,在开始一个疯狂的项目之前,我想知道如何开始,在场景之间切换.我看到有几个功能,启动,启动,切换,运行,继续,暂停等...

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...

例如,假设我要有2个场景,一个菜单和一个游戏.我在菜单上启动,我想转到游戏场景,如果我单击一个按钮,则返回菜单场景.我已经通过调用start函数实现了这一点,但是我注意到每次都调用所有initpreloadcreate函数,因此我要加载所有图像,一遍又一遍地设置所有侦听器再次.

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?

谢谢.

推荐答案

这个问题可能有点宽泛,但是考虑到Phaser 3,它仍然取决于菜单的作用.

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.

使用Phaser 3支持多个场景-使用 Dev Log#119 Dev Log#121 可能是目前最好的信息来源-另一个选择是在其中启动一个新场景当前的场景来解决这个问题.

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.

但是,如果这真的只是UI,则没有什么可以阻止您创建叠加层,而不是生成整个场景.

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.

我如何在我的入门模板是创建场景时将场景添加到场景管理器中.然后我按start过渡到第一个场景.

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);

然后我只需根据需要保持start下一个场景.

Then I simply keep starting the next scenes as needed.

this.scene.start(Preloader.Name);

对于另一个使用多个场景的游戏,我最终创建了以下函数(TypeScript)来处理此问题:

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).

这篇关于Phaser3场景过渡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆