Skip to content

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

  • Move to target position
    moveTo.moveTo(x, y);
    
    or
    moveTo.moveTo({
        x: 0,
        y: 0,
        // speed: 0
    });
    
    • x , y : Target position
  • Move from start position to current position
    moveTo.moveFrom(x, y);
    
    or
    moveTo.moveFrom({
        x: 0,
        y: 0,
        // speed: 0
    });
    
    • x , y : Start position
  • Move toward angle
    moveTo.moveToward(angle, distance);
    
    • angle : Angle in radian.

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;