Hi,
I’m trying to follow this example for adding a button to my scene:
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
function preload() {
game.load.atlas('button', 'assets/buttons/button_texture_atlas.png', 'assets/buttons/button_texture_atlas.json');
game.load.image('background','assets/misc/starfield.jpg');
}
var button;
var background;
function create() {
game.stage.backgroundColor = '#182d3b';
background = game.add.tileSprite(0, 0, 800, 600, 'background');
button = game.add.button(game.world.centerX - 95, 400, 'button', actionOnClick, this, 'over', 'out', 'down');
This file has been truncated. show original
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?
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
samme
April 14, 2020, 1:39pm
12
Eventually you would want setFrame(…)
instead.