gpt4 book ai didi

typescript - 使用 Typescript 的 Phaser 游戏设置

转载 作者:搜寻专家 更新时间:2023-10-30 21:31:00 24 4
gpt4 key购买 nike

我正在开始一个游戏,我想使用 Phaser 作为我的游戏框架。除此之外,我当前的技术栈还包括 Typescript 和 Webpack。但是,我的问题是,当我尝试如下扩展 Phaser Game 类 Phaser.Game 时,我无法让 Phaser 工作:

export class Game extends Phaser.Game {
sprite: Phaser.Sprite;

constructor(public width: number, public height: number) {
super(width, height, Phaser.AUTO, 'game', null);

this.state.add('Boot', Boot, false);

this.state.start('Boot');
}

preload(): void {
console.log('preload');
}

create(): void {
console.log('create');
}

update(): void {
console.log('update');
}

render(): void {
console.log('render');
}
}

这是大多数人的做法。尽管如此,似乎除了构造函数之外我的函数都没有被调用。除了我非常喜欢的上述方法外,我看到人们改为执行以下操作:

game: Phaser.Game;

constructor() {
this.game = new Phaser.Game(1280, 720, Phaser.AUTO, 'content', {
create: this.create, preload: this.preload
});
}

基本上,它们定义了一个 Phaser.Game 变量并创建了一个新实例,并在构造函数中传递了这些方法。这确实有效,但我想知道为什么前者不起作用。我错过了什么吗?

最佳答案

我的设置是这样的:

游戏

export class Game extends Phaser.Game {

constructor() {

var renderMode: number = Phaser.AUTO;

super(100, 100, renderMode, "content", null);

//add the states used the first will automatically be set
this.state.add(GameStates.BOOT, Boot, true);
this.state.add(GameStates.PRELOADER, Preloader, false);
this.state.add(GameStates.MAINMENU, MainMenu, false);
this.state.add(GameStates.GAME, SwipeEngine, false);

}

}

可选状态常量

export class GameStates {

static BOOT: string = "boot";
static PRELOADER: string = "preloader";
static MAINMENU: string = "mainMenu";
static GAME: string = "game";

}

每个州

export class Boot extends Phaser.State {

preload() {

console.log("Boot::preload");


this.load.image("blah");
this.load.image("blah2");

this.load.json("blah"), true);

}

create() {

console.log("Boot::create");


//start the preloader state
this.game.state.start(GameStates.PRELOADER, true, false);

}

shutdown(): void {

console.log("Boot::shutDown");

}

}

关于typescript - 使用 Typescript 的 Phaser 游戏设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35665047/

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