Rotate to

Introduction

Rotate game object towards target position with a steady speed.

  • Author: Rex
  • Behavior of game object

Usage

Sample code

Install plugin

Load minify file

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

Import plugin

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

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Import class
    import RotateTo from 'phaser3-rex-plugins/plugins/rotateto.js';
    
  • Add rotate-to behavior
    var rotateTo = new RotateTo(gameObject, config);
    

Create instance

var rotateTo = scene.plugins.get('rexRotateTo').add(gameObject, {
    // speed: 180
});
  • speed : Turn speed, degrees in second

Start moving

Rotate game object towards position

  • Shortest angle
    rotateTo.rotateTowardsPosition(x, y);
    
    • x , y : Target position
  • Clockwise/counter-clockwise
    rotateTo.rotateTowardsPosition(x, y, dir);
    
    • dir :
      • 0 : Shortest angle
      • 1, or 'cw' : Clockwise
      • 2, or 'ccw' : Counter-clockwise

Rotate game object to angle

  • Shortest angle
    rotateTo.rotateTo(angle);
    
    • angle : Target angle in degrees
  • Clockwise/counter-clockwise
    rotateTo.rotateTo(angle, dir);
    // rotateTo.rotateTo(angle, dir, speed);
    
    • dir :
      • 0 : Shortest angle
      • 1, or cw : Clockwise
      • 2, or ccw : Counter-clockwise

Enable

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

Pause, Resume, stop rotating

rotateTo.pause();
rotateTo.resume();
rotateTo.stop();

Set speed

rotateTo.setSpeed(speed);
// rotateTo.speed = speed;
  • speed : Turn speed, degrees in second

Events

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

Status

  • Is moving
    var isRunning = rotateTo.isRunning;