Phaser.State未初始化
问题描述:
我正在创建我的第一个Phaser游戏作为chromecast接收器应用程序,我在使用我的代码时遇到了一些麻烦。Phaser.State未初始化
我有下面的代码工作:
class TNSeconds {
game: Phaser.Game;
constructor() {
this.game = new Phaser.Game(window.innerWidth * window.devicePixelRatio -20, window.innerHeight * window.devicePixelRatio -20, Phaser.CANVAS, 'content', { preload: this.preload, create: this.create });
}
preload() {
this.game.load.image('BG', 'bg.png');
this.game.load.atlas("Atlas", "atlas.png", "atlas.json");
}
create() {
var background= this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'BG');
logo.anchor.setTo(0.5, 0.5);
this.game.add.sprite(320, 100, "Atlas", "dron1", this.game.world);
}
}
window.onload =() => {
var game = new TNSeconds();
};
不过我下面的教程和例子奠定了作为这样的代码:
class Game extends Phaser.Game {
constructor() {
// init game
super(window.innerWidth * window.devicePixelRatio - 20, window.innerHeight * window.devicePixelRatio - 20, Phaser.CANVAS, 'content', State);
}
}
class State extends Phaser.State {
preload() {
this.game.load.image('BG', 'bg.png');
this.game.load.atlas("Atlas", "atlas.png", "atlas.json");
}
create() {
this.add.image(0, 0, "BG");
this.add.sprite(320, 100, "Atlas", "dron1", this.world);
}
}
window.onload =() => {
var game = new Game();
};
的教程代码看起来更干净,只是为了翻译教程,我希望类似地实现我的代码,问题似乎是State
类没有初始化,有没有人可以为我解释这一点。
我知道教程代码是使用this.add.image
我在使用this.game.add.sprite
这不是问题。
答
试着这么做:
game.ts
module Castlevania {
export class Game extends Phaser.Game {
constructor() {
super(800, 600, Phaser.AUTO, 'content', null);
this.state.add('Boot', Boot, false);
this.state.add('Preloader', Preloader, false);
this.state.add('MainMenu', MainMenu, false);
this.state.add('Level1', Level1, false);
this.state.start('Boot');
}
}
}
boot.ts
module Castlevania {
export class Boot extends Phaser.State {
preload() {
this.load.image('preloadBar', 'assets/loader.png');
}
create() {
// Unless you specifically need to support multitouch I would recommend setting this to 1
this.input.maxPointers = 1;
// Phaser will automatically pause if the browser tab the game is in loses focus. You can disable that here:
this.stage.disableVisibilityChange = true;
if (this.game.device.desktop) {
// If you have any desktop specific settings, they can go in here
this.stage.scale.pageAlignHorizontally = true;
}
else {
// Same goes for mobile settings.
}
this.game.state.start('Preloader', true, false);
}
}
}
你可以找到一个完全工作的例子here。
我试图按照这个例子,甚至使用不到6个月前更新的代码,我发布了这些问题:https://stackoverflow.com/questions/32802777/打字稿扩展关键字不工作 – Johntk
我解决了我链接的其他帖子上的问题,感谢您的意见。 – Johntk