This.add.image is not a function Phaser 3.16

Hello,

I working with the Zenva Tutorials and I have ran into an issue.

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);
        });
        

    }
}

Any help would be appreciated!

Cheers

Hi
Phaser 3.16 is old :confused:
Try this.scene.add.image

1 Like

I know 3.16 is old, but it was the one that was used in the tutorial.

Also, that WORKED!

Is that because I had this.scene.add.existing(this) in my constructor?

Thanks again!

No, your UIbutton extends a container, and this.scene.add.existing(this) add this container to the scene

I ran into the same problem and wanted to add that this question has to do with a Zenva course called “Intro to RPG Development with Phaser”, lesson 15. The video was partially outdated. After finding the solution here (thank you BluntT76!) I realized that there was also an update in the course lesson notes at Zenva, but it was not obvious that there were any notes there.