单击图像后无法在 Phaser 3 中更改场景
Cannot change scene in Phaser 3 after clicking an image
我试图通过设置点击事件在 Phaser 3 中从一个场景导航到另一个场景,但是调用 this.scene.start('entryLevel');
时出现问题,当我点击图像时我得到:
this.scene.start
is not a function
我不知道为什么,我该如何解决这个问题?这是我的代码:
class MainMenu extends Phaser.Scene {
constructor() {
super('bootGame')
}
preload() {
this.load.image('menuBackground', 'assets/world/menubackground.png');
this.load.image('play_button', 'assets/world/play_button.png');
this.load.audio('menu_music', 'assets/music/menu_music.mp3');
}
onObjectClicked() {
this.scene.start('entryLevel');
}
create() {
this.add.image(400, 300, 'menuBackground');
var playButton = this.add.image(this.game.renderer.width / 2, this.game.renderer.height / 3, 'play_button').setDepth(1);
playButton.setInteractive();
this.input.on('gameobjectdown', this.onObjectClicked);
this.sound.play('menu_music', {
loop: true
});
}
}
我认为这可能是一个范围界定问题。
尝试以下方法是否适合您:
const self = this;
this.input.on('gameobjectdown', function () {
self.scene.start('entryLevel');
});
希望对您有所帮助!
我试图通过设置点击事件在 Phaser 3 中从一个场景导航到另一个场景,但是调用 this.scene.start('entryLevel');
时出现问题,当我点击图像时我得到:
this.scene.start
is not a function
我不知道为什么,我该如何解决这个问题?这是我的代码:
class MainMenu extends Phaser.Scene {
constructor() {
super('bootGame')
}
preload() {
this.load.image('menuBackground', 'assets/world/menubackground.png');
this.load.image('play_button', 'assets/world/play_button.png');
this.load.audio('menu_music', 'assets/music/menu_music.mp3');
}
onObjectClicked() {
this.scene.start('entryLevel');
}
create() {
this.add.image(400, 300, 'menuBackground');
var playButton = this.add.image(this.game.renderer.width / 2, this.game.renderer.height / 3, 'play_button').setDepth(1);
playButton.setInteractive();
this.input.on('gameobjectdown', this.onObjectClicked);
this.sound.play('menu_music', {
loop: true
});
}
}
我认为这可能是一个范围界定问题。
尝试以下方法是否适合您:
const self = this;
this.input.on('gameobjectdown', function () {
self.scene.start('entryLevel');
});
希望对您有所帮助!