Shake position

Introduction

Shake position of game object.

  • Author: Rex
  • Behavior of game object

Live demos

Usage

Sample code

Install plugin

Load minify file

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

Import plugin

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

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Import class
    import ShakePosition from 'phaser3-rex-plugins/plugins/shakeposition.js';
    
  • Add shake-position behavior
    var shakePosition = new ShakePosition(gameObject, config);
    

Create instance

var shake = scene.plugins.get('rexShake').add(gameObject, {
    // mode: 1, // 0|'effect'|1|'behavior'
    // duration: 500,
    // magnitude: 10,
    // magnitudeMode: 1, // 0|'constant'|1|'decay'
});
  • mode :
    • 'effect', or 0 : Shake position in 'poststep' game event, and restore in 'step' game event.
    • 'behavior', or 1 : Shake position in 'preupdate' scene event.
  • duration : Duration of shaking, in millisecond.
  • magnitude : The strength of the shake, in pixels.
  • magnitudeMode :
    • 'constant', or 0 : Constant strength of the shake.
    • 'decay', or 1 : Decay the strength of the shake.

Start shaking

shake.shake();
// shake.shake(duration, magnitude);

or

shake.shake({
    duration: 500,
    magnitude: 10
});

Stop shakeing

shake.stop();

Enable

  • Enable/resume (default)
    shake.setEnable();
    
    or
    shake.enable = true;
    
  • Disable/pause
    shake.setEnable(false);
    
    or
    shake.enable = false;
    

Set updating mode

shake.setMode(mode);
  • mode :
    • 'effect', or 0 : Shake position in post-update stage, and restore in pre-update stage.
    • 'behavior', or 1 : Shake position in pre-update stage.

Set duration

shake.setDuration(duration);
// shake.duration = duration;

Set magnitude

shake.setMagnitude(magnitude);
shake.magnitude = magnitude;
  • magnitude : The strength of the shake, in pixels.

Set magnitude mode

shake.setMagnitudeMode(magnitudeMode);
// shake.magnitudeMode = magnitudeMode;
  • magnitudeMode :
    • 'constant', or 0 : Constant strength of the shake.
    • 'decay', or 1 : Decay the strength of the shake.

Events

  • On reached target
    shake.on('complete', function(shake, gameObject){});
    

Status

  • Is shakeing
    var isRunning = shake.isRunning;