简体   繁体   English

加载函数中的phaser js未定义变量

[英]phaser js undefined variable within loading function

I'm just starting out using phaser JS for game dev but have come across a an odd issue which I need some help on. 我刚开始使用Phaser JS进行游戏开发,但是遇到了一个奇怪的问题,我需要一些帮助。

Let me show my code and explain where things are going wrong: 让我展示我的代码并解释哪里出了问题:

 class SimpleGame { game: Phaser.Game; csvTM: string; constructor (csvTM: string) { this.csvTM = csvTM; this.game = new Phaser.Game(800, 600, Phaser.AUTO, 'content', { preload: this.preload, create: this.create }); } test() { console.log("map test: " + this.csvTM); } preload() { console.log("map preload: " + this.csvTM); this.game.load.image('logo', 'empty_room.png'); this.game.load.tilemap("ItsTheMap", this.csvTM, null, Phaser.Tilemap.CSV); this.game.load.image("Tiles", "concrete.png"); } create() { console.log("map create: " + this.csvTM); var map = this.game.add.tilemap('ItsTheMap', 32, 32); map.addTilesetImage("Tiles"); var layer = map.createLayer(0); layer.resizeWorld(); } } 

Now what i'm trying to do here is simply pass a csv file path to the constructor of the SimpleGame object. 现在,我在这里要做的只是将一个csv文件路径传递给SimpleGame对象的构造函数。 When just using an absolute path, everything works just fine and i'l able to see the grid, etc. The issue comes in when I try to use the variable. 当仅使用绝对路径时,一切工作正常,并且我能够看到网格等。当我尝试使用变量时,问题就来了。 Notice I have three log statements all displaying the contents of csvTM variable. 注意,我有三个日志语句,所有语句均显示csvTM变量的内容。 Now what I do is first: 现在我要做的是首先:

 var game = new SimpleGame(msg["Commands"][0][1]); game.test(); 

which loads the new phaser object and passes the file path into the constructor. 它将加载新的相位器对象,并将文件路径传递到构造函数中。 Now im 100% sure the local variable csvTM is set becuase when I do teh game.test() above I see the file path. 现在,我100%确保在执行上面的game.test()时看到文件路径时,已设置局部变量csvTM。 However in preload and create the csvTM is always undefined.. thus breaking my code. 但是,在预加载和创建中,csvTM始终是不确定的..因此破坏了我的代码。 I do notice that the game object which is also made in the constructor seems to work and is always defined. 我确实注意到,同样在构造函数中创建的游戏对象似乎可以正常工作并且始终处于定义状态。

Does anyone know why my local variable string is undefined in only preload and create while the game local variable seems to be defined? 有谁知道为什么我的局部变量字符串仅在预加载和创建时未定义,而游戏局部变量似乎已定义?

Thanks for your help! 谢谢你的帮助!

It is hard for me to be sure about this since I do not have access to all of your code. 由于我无法访问您的所有代码,因此我很难确定这一点。 But I think it might be a case of the classic javascript "this" problem. 但我认为这可能是经典javascript“此”问题的一种情况。

Try defining the methods as: 尝试将方法定义为:

preload = () =>  { ... }
create = () =>  { ... }

This will make sure that "this" inside the functions is actually the correct "version" of "this". 这将确保函数中的“ this”实际上是“ this”的正确“版本”。

There are many questions (and answers) about how "this" works in typescript/javascript on stackoverflow but my answer to a previous similar question might explain why you are getting this behaviour: Visual Studio shows wrong value for `this` in TypeScript 关于stackoverflow上的typescript / javascript中“ this”的工作方式,存在很多问题(和答案),但是我对先前类似问题的回答可能解释了为什么您会得到这种行为: Visual Studio在TypeScript中显示 “ this”的值错误

EDIT: 编辑:

As Marwane KA pointed out in his comment to my answer the issue can be solved by replacing 正如Marwane KA在他对我的回答的评论中指出的那样,可以通过替换来解决问题

{ preload: this.preload, create: this.create }

in the initiation with "this". 以“ this”开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM