this.add.image不是一个函数/w Phaser 3-3.16



我试图在标题屏幕中添加一个按钮,但我得到了这个。add.image不是一个函数。

我把图片换成了其他的措辞,什么也没发生。我正在使用Zenva的学院,并遵循教程,遇到了这个问题。

这是我的代码

class UiButton extends Phaser.GameObjects.Container {
constructor(scene, x, y, key, hoverKey, text, targetCallback){
super(scene, x, y);
this.scene = scene; // the scene this container will be added to
this.x = x; // x pos
this.y = y; // y pos
this.key = key; // BG image of button
this.hoverKey = hoverKey; // image displays when hovered
this.text = text; // text displayed on button
this.targetCallback = targetCallback; // the callback function that will be called when clicked

this.createButton(); // Creates UI Button
this.scene.add.existing(this); // adds this container to our Phaser Scene
}
createButton() {
//create Play Game Button
this.button = this.add.image(0, 0, "button1");

//make button interactive
this.button.setInteractive();
//scale the button
this.button.setScale(1.4);

//create button text
this.buttonText = this.add.text(0, 0, this.text, {fontSize: "26px", fill: "#fff"});
//center text in ui button
Phaser.Display.Align.In.Center(this.buttonText, this.button);
//add the two game objects to container
this.add(this.button);
this.add(this.buttonText);
//Listen for events
this.button.on("pointerdown", () => {
this.targetCallback();
});
this.button.on("pointerover", () => {
this.button.setTexture(this.hoverKey);
});

this.button.on("pointerout", () => {
this.button.setTexture(this.key);
});

}
}

有什么想法吗?

谢谢!

容器对象有一个add(child)方法。然而,它需要一个现有的游戏对象。我认为您正在尝试构建图像,并且需要使用场景(场景中的工厂(来完成此操作。因此,您应该尝试:

this.button = this.scene.add.image(0, 0, "button1");

文本也是如此:

this.buttonText = this.scene.add.text(0, 0, this.text, {fontSize: "26px", fill: "#fff"});

最新更新