Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php on line 101

Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks/edd-blocks.php on line 101

Warning: The magic method GAINWP_Manager::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/gainwp.php on line 78

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 555

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 585

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 617

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 651

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 686

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 745

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 785

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":5884,"date":"2018-05-26T04:49:22","date_gmt":"2018-05-26T04:49:22","guid":{"rendered":"https:\/\/phasergames.com\/?p=5884"},"modified":"2020-01-14T16:32:07","modified_gmt":"2020-01-14T09:32:07","slug":"how-to-make-buttons-in-phaser-3","status":"publish","type":"post","link":"https:\/\/phasergames.com\/how-to-make-buttons-in-phaser-3\/","title":{"rendered":"how to make buttons in Phaser 3 with sprite sheets"},"content":{"rendered":"

Make Buttons in Phaser 3<\/h1>\n

We all need a way to easily add buttons to our game. While Phaser CE had this built in, Phaser 3 does not. I understand why resources were put elsewhere since we can always just use a sprite anyway. It is nice to have a way, however, to quickly make buttons in Phaser 3 with rollover and press states.<\/p>\n

What about mobile?<\/h2>\n

Sure mobile devices don’t have rollovers, but it still can show the downstate. The user needs to know that the button was pressed. It is good practice to give feedback on every action the user takes.<\/p>\n

So What do we need to do?<\/h3>\n

To make buttons in Phaser 3, there are several ways to go about it. In Phaser CE, I might have used a group. Phaser 3 groups do not have the ability to set an x or a y position. Phaser Containers have this ability and they would be the best choice if I needed to have more than one element. Since I am going to keep this as close to the classic Phaser CE method, I will use a sprite sheet and create a class that extends a sprite.<\/p>\n

\"how<\/p>\n

Using the button class<\/h2>\n

Normally I would set everything up in the class before I would show how to use it in your own code, but in this case, I think that it is helpful to see the properties we are going to pass down into the class to show how we make the buttons in Phaser 3. I’ll be using the basic phaser 3 template to build this project.<\/p>\n

The first thing we will do is to preload the sprite sheet which each cell is 236 px wide and 65 px high<\/p>\n

this.load.spritesheet(\"buttons\", \"images\/buttons.png\",{ frameWidth: 236, frameHeight: 65 });<\/pre>\n

This is how we create the button. As you can see from the example below we are using an object rather than parameters. This is in keeping with the syntax of Phaser 3 and we can also put the properties in any order.<\/p>\n

var button = new BasicButton({\n            'scene': this,\n            'key':'buttons',\n            'up': 0,\n            'over':1,\n            'down':2,\n            'x': 240,\n            'y': 480\n        })<\/pre>\n

The Basic Button Class<\/h2>\n

Next, make a new file called BasicButton.js and put this code into it.<\/p>\n

class BasicButton extends Phaser.GameObjects.Sprite {\n\n\n}<\/pre>\n

Now we have an empty class that extends a sprite, which means it has all the properties and methods of a sprite, plus the custom ones we will add. Before we go any further, we need to add the constructor which is called when a new instance of the basic button class is created.<\/p>\n

class BasicButton extends Phaser.GameObjects.Sprite {\n    constructor(config) {\n    \n    \n    }\n}<\/pre>\n

The constructor is passed the single parameter of the config object which contains all of our buttons properties which are<\/p>\n