gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:16:43 27 4
gpt4 key购买 nike

在 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 之间有一个区别:

  • 在我的本地示例中,game 属性在 MyScene 实例中不可用;
  • 在在线沙箱中,我可以看到此属性存在(通过添加相同的 console.log(this))。

我注意到我可以使用 Phaser.Scene.sys.game 获取 Game 实例,但我想知道为什么我在在线沙箱和我自己的例子。

您可以在 Phaser 3 API Documentation 中阅读此内容关于 Phaser.Scene.game:

game :Phaser.Game

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

所以我尝试修改 MyScene 构造函数中的 Scene Injection Map,但我现在还不太了解它,遗憾的是我所有的尝试都失败了。

为了使 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.

关于javascript - Phaser.Scene : how to make the `game` property available?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51853637/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com