Button

Introduction

Fires 'click' event when touch releasd after pressed.

  • Author: Rex
  • Behavior of game object

Usage

Sample code

Install plugin

Load minify file

  • Load plugin (minify file) in preload stage
    scene.load.plugin('rexbuttonplugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rexbuttonplugin.min.js', true);
    
  • Add button behavior
    var button = scene.plugins.get('rexbuttonplugin').add(gameObject, config);
    

Import plugin

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Install plugin in configuration of game
    import ButtonPlugin from 'phaser3-rex-plugins/plugins/button-plugin.js';
    var config = {
        // ...
        plugins: {
            global: [{
                key: 'rexButtonn',
                plugin: ButtonPlugin,
                start: true
            },
            // ...
            ]
        }
        // ...
    };
    var game = new Phaser.Game(config);
    
  • Add button behavior
    var button = scene.plugins.get('rexButtonn').add(gameObject, config);
    

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Import class
    import Button from 'phaser3-rex-plugins/plugins/button.js';
    
  • Add button behavior
    var button = new Button(gameObject, config);
    

Create instance

var button = scene.plugins.get('rexButton').add(gameObject, {
    // enable: true,
    // mode: 1,            // 0|'press'|1|'release'
    // clickInterval: 100  // ms
});
  • enable : drag-able
  • mode :
    • 'pointerdown', 'press', or 0 : Fire 'click' event when touch pressed.
    • 'pointerup', 'release', or 1 : Fire 'click' event when touch released after pressed.
  • clickInterval : Interval between 2 'click' events, in ms.

Events

  • Click
    button.on('click', function (button, gameObject, pointer, event) {
        // ...
    }, scope);
    
    • Cancel remaining touched events : event.stopPropagation()
  • Enable
    button.on('enable', function (button, gameObject) {
        // ...
    }, scope);
    
  • Disable
    button.on('disable', function (button, gameObject) {
        // ...
    }, scope);
    

Enable

  • Get
    var enabled = button.enable;  // enabled: true, or false
    
  • Set
    button.setEnable(enabled);  // enabled: true, or false
    // button.enable = enabled;
    
  • Toggle
    button.toggleEnable();
    

Set mode

button.setMode(mode);
  • mode :
    • 'press', or 0 : Fire 'click' event when touch pressed.
    • 'release', or 1 : Fire 'click' event when touch released after pressed.

Set click interval

button.setClickInterval(interval);  // interval in ms