【问题标题】:Switching between scenes in Phaser3在 Phaser3 中的场景之间切换
【发布时间】:2020-04-13 19:48:57
【问题描述】:

所以我的 Phaser3package.json 中的版本是 3.21.0)应用程序中有两个场景,我想在没有任何侧面的情况下在这些场景之间切换 -影响。第一个场景是处理高分菜单,第二个场景是游戏本身。

相关代码

app.ts 文件如下所示:

const config = {
    title: '<game-name>',
    width: 800,
    height: 600,
    parent: 'game',
    scene: [MenuScene, GameScene],
};

export class GameName extends Phaser.Game {
    constructor(config: any) {
        super(config);
    }
}

window.onload = () => new GameName(config);

MenuScene.ts - 去掉了不适用的部分:

class MenuScene extends Phaser.Scene {
    constructor() { super({key: 'MenuScene'}); }

    create():void {
       console.log('create menuscreen');
       const enter = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER);
       enter.on('down', () => { this.scene.start('GameScene'); }
    }
}

GameScene.ts——也去掉了不相关的代码sn-ps:

class GameScene extends Phaser.Scene {
    constructor() { super({key: 'GameScene'}); }

    create = ():void => {
       console.log('create gamescreen');
       const esc = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ESC);
       esc.on('down', () => { this.scene.start('MenuScene'); }
    }
}

问题和我的问题

因此,一旦我重复按 EnterEsc 只是为了使用提供的解决方案进行切换,它似乎并没有清理后台的场景。在console 上,我在切换几次后看到以下内容(在下面的示例中为 6 次):

我已经尝试在this.scene.start('&lt;scene-key&gt;') 之前调用this.scene.stop(),但仍然是同样的问题。我已经在阅读Phaser3 Scenes transitions 问题,但它似乎没有回答我的问题。有人告诉start() 应该清理或关闭未使用的。

那么我应该如何正确切换场景或清理当前未使用的场景?

谢谢!

【问题讨论】:

    标签: typescript phaser-framework game-development


    【解决方案1】:

    我就是这样做的

    1.- 暂停游戏的场景

    场景scene_pause 将在game_scene 之上,因为最后一个场景将是顶部场景

    isPaused = this.scene.isPaused('scene_game');
    if (false === isPaused) {
        this.scene.pause('scene_game');
    }
    const isNull = this.scene.get('scene_pause');
    if (null === isNull) {
        this.scene.add('pause_scene', PauseScene, true);
    }
    

    然后当你恢复游戏时

    isActive = this.scene.isActive('scene_pause');
    if (true === isActive) {
        this.scene.remove('scene_pause');
    }
    isPaused = this.scene.isPaused('scene_game');
    if (true === isPaused) {
        this.scene.resume('scene_game');
    }
    

    2.- 游戏结束切换场景

    this.scene.stop('scene_game'); // stop executing the Update 1rst
    ... // save the state and score
    this.scene.remove('scene_game'); // I remove the scene, because I will add again when start the game
    this.scene.stop('scene_ui');
    this.scene.switch('scene_game_over');
    

    【讨论】:

    • 所以我申请了add(如果不存在)和remove,然后开始了另一个成功的场景。感谢您的帮助!
    猜你喜欢
    • 2019-04-13
    • 2016-09-09
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多