Player

Introduction

Player of T ime-C ommand-R ecorder-P layer, to run commands on time.

  • Author: Rex
  • Member of scene

Live demos

Usage

Sample code

Install plugin

Load minify file

  • Load plugin (minify file) in preload stage
    scene.load.plugin('rextcrpplugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rextcrpplugin.min.js', true);
    
  • Create instance
    var player = scene.plugins.get('rextcrpplugin').addPlayer(scene, config);
    

Import plugin

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Install plugin in configuration of game
    import TCRPPlugin from 'phaser3-rex-plugins/plugins/tcrp-plugin.js';
    var config = {
        // ...
        plugins: {
            global: [{
                key: 'rexTCRP',
                plugin: TCRPPlugin,
                start: true
            },
            // ...
            ]
        }
        // ...
    };
    var game = new Phaser.Game(config);
    
  • Create instance
    var player = scene.plugins.get('rexTCRP').addPlayer(scene, config);
    

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Import class
    import TCRP from 'phaser3-rex-plugins/plugins/tcrp.js';
    
  • Create instance
    var player = new TCRP.Player(scene, config);
    

Create instance

var player = scene.plugins.get('rexTCRP').addPlayer(scene, {
    // timeUnit: 0,        // 'ms'|0|'s'|'sec'|1
    // dtMode: 0,          // 'abs'|'absolute'|0|'inc'|'increment'|1
    // commands: [],       // [[dt, command], [dt, command], ...]
    // timeScale: 1,       // 0~1
    // scope: undefined
});

Load commands

player.load(commands, scope, {
    // timeUnit: 0,        // 'ms'|0|'s'|'sec'|1
    // dtMode: 0           // 'abs'|'absolute'|0|'inc'|'increment'|1
});
  • Commands : see also Run commands
    [
        [dt, command],
        [dt, command],
        ...
    ]
    
    • Format of each row :
      [dt, fnName, param0, param1, ...]
      // [dt, callback, param0, param1, ...]
      
      [dt, [fnName, param0, param1, ...]]
      // [dt, [callback, param0, param1, ...]]
      
      [dt, [command0, command1, ...]]
      
  • timeUnit: time-unit of dt
    • 'ms', or 0 : dt in millisecond
    • 's', 'sec', or 1 : dt in second
  • dtMode: mode of counting dt
    • 'abs', 'absolute', or 0 : timeout = dt
    • 'inc', 'increment', 1 : timeout = dt + previous-timeout

Start playing

player.start();
// player.start(startAt);  // start-at time in ms

Events

  • Complete
    player.on('complete', function(player){});
    
  • Run command
    player.on('runcommand', function(commands, scope){});
    

Pause, Resume, stop playing

player.pause();
player.resume();
player.stop();

Seek elapsed time

player.seek(time);   // elapsed time in ms

State of player

var isPlaying = player.isPlaying;
var completed = player.completed;
var now = player.now;

Time-scale

  • Set
    player.setTimeScale(value);  // 0~1
    // player.timeScale = value;
    
  • Get
    var timeScale = player.timeScale;