我正在创建我的第一个Phaser游戏作为chromecast接收器应用程序,我的代码遇到了一些问题。
我下面的代码有效:
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
这不是问题。
答案 0 :(得分:2)
尝试类似:
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');
}
}
}
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。