Can't Add Buttons To Scenes?

Hi,

I’m trying to follow this example for adding a button to my scene:

My button has a regular state, hover state, and down state so I would like to use this nice api:

button = game.add.button(game.world.centerX - 95, 400, 'button', actionOnClick, this, 'over', 'out', 'down');

When I am a scene I can to “this.add.image” and “this.add.sprite”, but for some reason “button” is not an addable thing here? Any reason why they decided not to implement this? :thinking:

When I try to do

this.game.add.button 

I get the error, “Property ‘add’ does not exist on type ‘Game’”

It’s an exemple for phaser2, phaser2 and 3 aren’t compatible

Hi,
What is your version of Phaser?

I’m using v3.22

Isn’t this a useful thing to have though? Not sure why it was never ported over to phaser 3…

Probably because any gameobject can act as a button, like a sprite or image.

hmm is there an easy way to swap between normal, hover, and down states like in the phaser2 api?

gameObject.setInteractive().on('pointerdown', function(pointer, localX, localY, event){
    // ...
});

And you have all this events available for your specific use:
https://photonstorm.github.io/phaser3-docs/Phaser.Input.Events.html#event:POINTER_OVER__anchor

normal:

gameObject.on('pointerout', function(...

hover:

gameObject.on('pointerover', function(...

down is what blunt showed

ok thanks, these are the events I need to listen to, but what is the proper way to swap out a sprite’s image for another one after it’s been created and added to the scene?

@JimLynchCodes
sprite.setTexture('key')

1 Like

Eventually you would want setFrame(…) instead.