Skip to content

Layer manager

Introduction

A dictionary to store Layer game objects.

  • Author: Rex
  • Container of game objects

Usage

Sample code

Install plugin

Load minify file

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

Import plugin

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Install plugin in configuration of game
    import LayerManagerPlugin from 'phaser3-rex-plugins/plugins/layermanager-plugin.js';
    var config = {
        // ...
        plugins: {
            global: [{
                key: 'rexLayerManager',
                plugin: LayerManagerPlugin,
                start: true
            },
            // ...
            ]
        }
        // ...
    };
    var game = new Phaser.Game(config);
    
  • Add layer manager
    var layerManager = scene.plugins.get('rexLayerManager').add(scene, config);
    

Import class

  • Install rex plugins from npm
    npm i phaser3-rex-plugins
    
  • Import class
    import LayerManager from 'phaser3-rex-plugins/plugins/layermanager.js';
    
  • Add move-to behavior
    var layerManager = new LayerManager(scene, config);
    

Create instance

var layerManager = scene.plugins.get('rexLayerManager').add(scene, {
    // layerManager: ['layer0', 'layer1', ...]
});

or

var layerManager = scene.plugins.get('rexLayerManager').add(scene, 
    ['layer0', 'layer1', ...]
);
  • layers : Add layers by name string array.

Destroy

layerManager.destroy()

Also destroy all layers in this layer manager.

Add layer

layerManager.add(name);
// layerManager.add(name, depth);

Add game object to layer

layerManager.addToLayer(name, gameObject);
  • name : Name of layer
  • gameObject : Any kind of game object, include containerLite and related game objects.

Note

Print a warn message if specific layer is not existed.

Get layer

  • Get layer by name
    var layer = layerManager.getLayer(name);
    
  • Get all layers in this layer manager
    var layers = layerManager.getLayers();
    // var out = layerManager.getLayers(out);
    

Has layer

var hasLayer = layerManager.has(name);