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 class

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 poolable(object) → {Boolean}

Check if an object is poolable (was properly registered with the recycling feature enable)

Parameters:
Name Type Description
object Object
Returns:
Boolean -

true if the object is poolable

See:
Example
if (!me.pool.poolable(myCherryEntity)) {
    // object was not properly registered
}

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

Example
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, throwOnErroropt) → {Boolean}

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 Attributes Default Description
instance Object

to be recycled

throwOnError Boolean <optional>
true

throw an exception if the object cannot be recycled

Throws:

will throw an error if the object cannot be recycled

Returns:
Boolean -

true if the object was successfully recycled in the object pool

static register(className, class, recyclingopt)

register an object to the pool.
Pooling must be set to true if more than one such objects will be created.
(Note: for an object to be poolable, it must implements a onResetEvent method)

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

recycling Boolean <optional>
false

enables object recycling for the specified class

Example
// implement CherryEntity
class CherryEntity extends Spritesheet {
   onResetEvent() {
       // reset object mutable properties
       this.lifeBar = 100;
   }
};
// add our users defined entities in the object pool and enable object recycling
me.pool.register("cherryentity", CherryEntity, true);