Move to

Introduction

Move game object towards target position with a steady speed.

  • 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('rexmovetoplugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rexmovetoplugin.min.js', true);
    
  • Add move-to behavior
    var moveTo = scene.plugins.get('rexmovetoplugin').add(gameObject, config);
    

Import plugin

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

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Import class
    import MoveTo from 'phaser3-rex-plugins/plugins/moveto.js';
    
  • Add move-to behavior
    var moveTo = new MoveTo(gameObject, config);
    

Create instance

var moveTo = scene.plugins.get('rexMoveTo').add(gameObject, {
    // speed: 400,
    // rotateToTarget: false
});
  • speed : Moving speed, pixels in second.
  • rotateToTarget : Set true to change angle towards path.

Start moving

moveTo.moveTo(x, y);
  • x , y : Target position

or

var config = {
    x: 0,
    y: 0,
    // speed: 0
}
moveTo.moveTo(config);

Enable

  • Enable (default)
    moveTo.setEnable();
    
    or
    moveTo.enable = true;
    
  • Disable
    moveTo.setEnable(false);
    
    or
    moveTo.enable = false;
    

Pause, Resume, stop moving

moveTo.pause();
moveTo.resume();
moveTo.stop();

Set speed

moveTo.setSpeed(speed);
// moveTo.speed = speed;

Set rotate-to-target

moveTo.setRotateToTarget(rotateToTarget);
  • rotateToTarget : Set true to change angle towards target

Events

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

Status

  • Is moving
    var isRunning = moveTo.isRunning;