When you have a class whose instance requires frequent creation and deletion, then it's the prefect case to put them into cc.pool
while deleting and reuse them when you need a new instance. The advantage of using objects pool is reducing memory usage and improve performance.
-
Make your class support
cc.pool
Firstly, you mast have a class with
unuse
andreuse
functions,cc.pool
will callunuse
function when you put it into the pool, and callreuse
function when you retrieve an object from the pool to reinitialize it with the given parameters.Here is a example class
MySprite
, we will use it later on to explain other functionalities.var MySprite = cc.Sprite.extend({ _hp: 0, _sp: 0, _mp: 0, ctor: function (f1, f2, f3) { this._super(f1, f2, f3); this.initData(f1, f2, f3); }, initData: function (f1, f2, f3) { this._hp = f1; this._mp = f2; this._sp = f3; }, unuse: function () { this._hp = 0; this._mp = 0; this._sp = 0; this.retain();//if in jsb this.setVisible(false); this.removeFromParent(true); }, reuse: function (f1, f2, f3) { this.initData(f1, f2, f3); this.setVisible(true); } }); MySprite.create = function (f1, f2, f3) { return new MySprite(f1, f2, f3) } MySprite.reCreate = function (f1, f2, f3) { var pool = cc.pool; if (pool.hasObject(MySprite)) return pool.getFromPool(MySprite, f1, f2, f3); return MySprite.create(f1, f2, f3); }
-
Put an object into
cc.pool
cc.pool.putInPool(object);
putInPool
function will invokeobject.unuse()
and put it intocc.pool
for future usage. -
Retrieve an object from
cc.pool
var object = cc.pool.getFromPool("MySprite", args);
By the time you wanted to get an object from the pool, you can use
getFromPool
by passing the class and parameters to retrieve an usable object or null if there is none. The pool will callreuse
function withargs
to initialize the object before return the it. -
Check whether there is an object available
var exist = cc.pool.hasObject("MySprite");
-
Remove an object from
cc.pool
cc.pool.removeObject(object);
-
Drain the pool
cc.pool.drainAllPools();
When you switch from a game scene to another, maybe the objects in old scene will be no longer useful. In this case, to avoid unnecessary memory usage, you can remove all objects in the pool with
drainAllPools
function.