me.pool

This object is used for object pooling - a technique that might speed up your game if used properly.
If some of your classes will be instantiated and removed a lot at a time, it is a good idea to add the class to this object pool. A separate pool for that class will be created, which will reuse objects of the class. That way they won't be instantiated each time you need a new one (slowing your game), but stored into that pool and taking one already instantiated when you need it.

This object is also used by the engine to instantiate objects defined in the map, which means, that on level loading the engine will try to instantiate every object found in the map, based on the user defined name in each Object Properties

Methods

static exists(name) → {Boolean}

Check if an object with the provided name is registered

Parameters:
Name Type Description
name String

of the registered object

Returns:
Boolean -

true if the classname is registered

static getInstanceCount() → {Number}

returns the amount of object instance currently in the pool

Returns:
Number -

amount of object instance

static pull(className) → {Object}

Pull a new instance of the requested object (if added into the object pool)

Parameters:
Name Type Attributes Description
className String

as used in me.pool.register

arguments... <optional>

arguments to be passed when instantiating/reinitializing the object

Returns:
Object -

the instance of the requested object

Examples
me.pool.register("player", PlayerEntity);
var player = me.pool.pull("player");
me.pool.register("bullet", BulletEntity, true);
me.pool.register("enemy", EnemyEntity, true);
// ...
// when we need to manually create a new bullet:
var bullet = me.pool.pull("bullet", x, y, direction);
// ...
// params aren't a fixed number
// when we need new enemy we can add more params, that the object construct requires:
var enemy = me.pool.pull("enemy", x, y, direction, speed, power, life);
// ...
// when we want to destroy existing object, the remove
// function will ensure the object can then be reallocated later
me.game.world.removeChild(enemy);
me.game.world.removeChild(bullet);

static purge()

purge the object pool from any inactive object
Object pooling must be enabled for this function to work
note: this will trigger the garbage collector

static push(instance)

Push back an object instance into the object pool
Object pooling for the object class must be enabled, and object must have been instantiated using me.pool#pull, otherwise this function won't work

Parameters:
Name Type Description
instance Object

to be recycled

static register(className, class, objectPoolingopt)

register an object to the pool.
Pooling must be set to true if more than one such objects will be created.
(note) If pooling is enabled, you shouldn't instantiate objects with new. See examples in me.pool#pull

Parameters:
Name Type Attributes Default Description
className String

as defined in the Name field of the Object Properties (in Tiled)

class Object

corresponding Class to be instantiated

objectPooling Boolean <optional>
false

enables object pooling for the specified class

  • speeds up the game by reusing existing objects
Example
// add our users defined entities in the object pool
me.pool.register("playerspawnpoint", PlayerEntity);
me.pool.register("cherryentity", CherryEntity, true);
me.pool.register("heartentity", HeartEntity, true);
me.pool.register("starentity", StarEntity, true);