Jump to content

How to create interactive buttons or how to change sprite.frame.name?


Recommended Posts

I've just started Phaser 3 development and I noticed there are no buttons, which I found a little odd. Buttons are a very important part of the interactivity of a game. In Phaser 2 I used them all the time for creating menus, select a level, restart level, quit game etc. But ok I understand that you can use a sprite and add .setInteractive() so it will act like a button.

The button should change color when on mouse-over and another color when it is pressed down. I'm using a sprite atlas with the different frames for the button faces, and I've tried using the pointerover and pointerout and pointerdown events to change the frame.name of the sprite but this doesn't seem to work.

	preload: function ()
		this.load.multiatlas('sprites', 'img/sprite_demo.json', 'img');

	create: function ()
		// back Button
		this.btnback = this.add.sprite(400, 480, 'sprites', 'btn_back').setInteractive();
		this.btnback.on('pointerover', function (ptr, x, y) { this.frame.name = 'btn_back_hl'} );
		this.btnback.on('pointerout', function (ptr)        { this.frame.name = 'btn_back'} );
		this.btnback.on('pointerdown', function(event){
			this.frame.name = 'btn_back_down';

Changing this.frame.name doesn't seem to do anything, it stays the same frame as when the sprite was created, no interactivity. So how can I make buttons work in Phaser 3?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...