Skip to content

Level counter

Introduction

Map level value from experience value, by callback or a number array.

  • Author: Rex
  • Object

Live demos

Usage

Sample code

Install plugin

Load minify file

  • Load plugin (minify file) in preload stage
    scene.load.plugin('rexlevelcounterplugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rexlevelcounterplugin.min.js', true);
    
  • Add level-counter object
    var levelCounter = scene.plugins.get('rexlevelcounterplugin').add(config);
    

Import plugin

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Install plugin in configuration of game
    import LevelCounterPlugin from 'phaser3-rex-plugins/plugins/levelcounter-plugin.js';
    var config = {
        // ...
        plugins: {
            global: [{
                key: 'rexLevelCounter',
                plugin: LevelCounterPlugin,
                start: true
            },
            // ...
            ]
        }
        // ...
    };
    var game = new Phaser.Game(config);
    
  • Add level-counter object
    var levelCounter = scene.plugins.get('rexLevelCounter').add(config);
    

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Import class
    import LevelCounter from 'phaser3-rex-plugins/plugins/levelcounter.js';
    
  • Add level-counter object
    var levelCounter = new LevelCounter(config);
    

Create instance

var levelCounter = scene.plugins.get('rexLevelCounter').add({
    table: function(level) { return level * 100; },
    // table: [0, 100, 200, 300,],

    // maxLevel: -1,

    // exp: 0,
});
  • table : Level table, return experience value from level value. Level value starts from 0.
    • A callback
      function(level) {
          return experience;
      }
      
    • A number array : Experience value of each level.
  • maxLevel :
    • undefined : Default value
      • No upper limit for callback level table.
      • (table.length - 1) for number array level table.
    • A number : Maximum level value
  • exp : Initial experience value. Default value is 0.

Accumulate experience

  • Accumulate experience
    levelCounter.gainExp(incExp);
    // levelCounter.exp += incExp;
    
    or
    levelCounter.gainExp(incExp, callback, scope);
    
    • Will fire 'levelup' event, or invoke callback many times.
      function(level, fromExp, toExp, levelStartExp, levelEndExp){        
      }
      
      • level : To next level
      • fromExp, toExp : Experience increment from fromExp to toExp.
      • levelStartExp, levelEndExp : Range of this level.
  • Reset experience value
    levelCounter.resetExp(exp);
    
    • Won't fire 'levelup' event.
  • Force level up
    levelCounter.setLevel(level);
    
    or
    levelCounter.setLevel(level, callback, scope);
    
    • Will fire 'levelup' event, or invoke callback many times.
      function(level, fromExp, toExp, levelStartExp, levelEndExp){        
      }
      
      • level : To next level
      • fromExp, toExp : Experience increment from fromExp to toExp.
      • levelStartExp, levelEndExp : Range of this level.

Level and experience

  • Get current experience
    var exp = levelCounter.getExp();
    // var exp = levelCounter.exp;
    
  • Get current level
    var level = levelCounter.getLevel();
    // var level = levelCounter.level;
    
  • Get current required experience to next level
    var exp = levelCounter.requiredExp;
    
  • Get experience of level
    var exp = levelCounter.getExp(level);
    
  • Get level from experience
    var level = levelCounter.getLevel(exp);
    
  • Get required experience to level
    var exp = levelCounter.getRequiredExpToNextLevel(level);
    // var exp = levelCounter.getRequiredExpToNextLevel(level, exp);
    

Events

  • Level-up when accumulating experience
    levelCounter.on('levelup', function(level, fromExp, toExp, levelStartExp, levelEndExp){        
    }, scope);
    
    • level : To next level
    • fromExp, toExp : Experience increment from fromExp to toExp.
    • levelStartExp, levelEndExp : Range of this level.