加载函数中的phaser js未定义变量
phaser js undefined variable within loading function
我刚开始使用 Phaser JS 进行游戏开发,但遇到了一个奇怪的问题,我需要一些帮助。
让我展示我的代码并解释哪里出了问题:
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();
}
}
现在我在这里要做的只是将一个 csv 文件路径传递给 SimpleGame 对象的构造函数。当仅使用绝对路径时,一切正常并且我能够看到网格等。当我尝试使用变量时出现问题。请注意,我有三个日志语句都显示 csvTM 变量的内容。现在我首先要做的是:
var game = new SimpleGame(msg["Commands"][0][1]);
game.test();
加载新的移相器对象并将文件路径传递给构造函数。现在我 100% 确定局部变量 csvTM 已设置,因为当我在上面执行 game.test() 时,我看到了文件路径。但是,在预加载和创建 csvTM 时始终未定义。因此破坏了我的代码。我注意到同样在构造函数中创建的游戏对象似乎可以工作并且总是被定义。
有谁知道为什么我的局部变量字符串仅在预加载和创建时未定义,而游戏局部变量似乎已定义?
感谢您的帮助!
我很难确定这一点,因为我无法访问您的所有代码。但我认为这可能是经典 javascript "this" 问题的一个例子。
尝试将方法定义为:
preload = () => { ... }
create = () => { ... }
这将确保函数内部的 "this" 实际上是 "this" 的正确 "version"。
关于 "this" 如何在 Whosebug 上的 typescript/javascript 中工作有很多问题(和答案),但我对之前类似问题的回答可能会解释为什么你会出现这种行为:
编辑:
作为 Marwane K.A。他在对我的回答的评论中指出这个问题可以通过替换
来解决
{ preload: this.preload, create: this.create }
与"this"开始。
我刚开始使用 Phaser JS 进行游戏开发,但遇到了一个奇怪的问题,我需要一些帮助。
让我展示我的代码并解释哪里出了问题:
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();
}
}
现在我在这里要做的只是将一个 csv 文件路径传递给 SimpleGame 对象的构造函数。当仅使用绝对路径时,一切正常并且我能够看到网格等。当我尝试使用变量时出现问题。请注意,我有三个日志语句都显示 csvTM 变量的内容。现在我首先要做的是:
var game = new SimpleGame(msg["Commands"][0][1]);
game.test();
加载新的移相器对象并将文件路径传递给构造函数。现在我 100% 确定局部变量 csvTM 已设置,因为当我在上面执行 game.test() 时,我看到了文件路径。但是,在预加载和创建 csvTM 时始终未定义。因此破坏了我的代码。我注意到同样在构造函数中创建的游戏对象似乎可以工作并且总是被定义。
有谁知道为什么我的局部变量字符串仅在预加载和创建时未定义,而游戏局部变量似乎已定义?
感谢您的帮助!
我很难确定这一点,因为我无法访问您的所有代码。但我认为这可能是经典 javascript "this" 问题的一个例子。
尝试将方法定义为:
preload = () => { ... }
create = () => { ... }
这将确保函数内部的 "this" 实际上是 "this" 的正确 "version"。
关于 "this" 如何在 Whosebug 上的 typescript/javascript 中工作有很多问题(和答案),但我对之前类似问题的回答可能会解释为什么你会出现这种行为:
编辑:
作为 Marwane K.A。他在对我的回答的评论中指出这个问题可以通过替换
来解决{ preload: this.preload, create: this.create }
与"this"开始。