Phaser 3不能让start方法正常工作



我试图制作一个菜单,当玩家使用开始方法点击按钮时,场景会发生变化。首先,我在create函数中使用如下代码:

var levelOne = this.add.sprite(200, 400, 'LevelOne').setInteractive();
levelOne.on('pointerdown', function (pointer) {
this.scene.start('play');

});

但是这会导致一个错误,它说this.scene.start不是一个函数。

我看了前面的一个例子,其中方法工作,最大的区别是该方法是在更新函数,所以我重写了我的代码,使其在创建函数:

this.choice = 0;
var levelOne = this.add.sprite(200, 400, 'LevelOne').setInteractive();
levelOne.on('pointerdown', function (pointer) {
this.choice = 1;
//game.settings = {
//gameTimer: 60000    
//}
});

在更新函数中:

if (this.choice == 1){
this.scene.start('play'); 
}

遗憾的是,这也不起作用,甚至没有给出错误消息。我不知道哪里出了问题。请帮助。

您必须将scene作为上下文传递给事件函数on(...)(链接到文档),作为第三个参数,以便您可以在事件回调中访问scene属性和函数。

levelOne.on('pointerdown', function (pointer) {
this.scene.start('play');    
}, this); // <-- you have to add "this"

最新更新