Phaser.Scene:如何让“游戏”属性可用?

Phaser.Scene: how to make the `game` property available?

在webpack/babel环境下,我运行一个类似这个的例子(在Phaser 3 Sandbox中可用);我刚刚添加了 console.log(this) 并删除了图像 load()add():

class MyScene extends Phaser.Scene {
    constructor (config)
    {
        super(config);
    }

    preload ()
    {
        console.log(this);
    }

    create ()
    {
    }
}

var config = {
    type: Phaser.WEBGL,
    width: 800,
    height: 600,
    backgroundColor: '#000000',
    parent: 'phaser-example',
    scene: MyScene
};

var game = new Phaser.Game(config);

它工作正常,但我自己的本地示例与在线 Phaser 3 Sandbox 之间有一处不同:

我注意到我可以使用 Phaser.Scene.sys.game 获取 Game 实例,但我想知道为什么在线沙箱和我自己的示例之间没有相同的结果对象。

您可以在关于 Phaser.Scene.gamePhaser 3 API Documentation 中阅读这篇文章:

game :Phaser.Game

A reference to the Phaser.Game instance.
This property will only be available if defined in the Scene Injection Map.

所以我尝试修改 MyScene 构造函数中的场景注入映射,但我现在还不太了解它,遗憾的是我所有的尝试都失败了。

我应该更改什么才能使 game 属性 可用?

我刚找到 this post 这似乎回答了这个问题:

Hi, Scenes have property game (Phaser.Game), but it seems to be undefined all the time (in preload, create or update methods of scene). Doc says: "This property will only be available if defined in the Scene Injection Map." and I can see it in default injection map ... but still undefined.

On the other side, there is sys property (object) with game property in it. I can get game reference like this.sys.game instead of this.game.

Is it correct to use this.sys.game? Why is not this.game working? Is it some leftover from older version?

管理员的回答:

Yeah it shouldn’t be exposed at all. I’ll remove it from the map. It should almost never be used.