Cocos Creator API

1.5.x

Cocos Creator is the game engine for the future.

Pool

Module: cc
Parent Module: js

A fixed-length object pool designed for general type.
The implementation of this object pool is very simple, it can helps you to improve your game performance for objects which need frequent release and recreate operations

examples:

Example 1:

function Details () {
   this.uuidList = [];
};
Details.prototype.reset = function () {
   this.uuidList.length = 0;
};
Details.pool = new JS.Pool(function (obj) {
   obj.reset();
}, 5);
Details.pool.get = function () {
   return this._get() || new Details();
};

var detail = Details.pool.get();
...
Details.pool.put(detail);

Example 2:

function Details (buffer) {
   this.uuidList = buffer;
};
...
Details.pool.get = function (buffer) {
   var cached = this._get();
   if (cached) {
       cached.uuidList = buffer;
       return cached;
   }
   else {
       return new Details(buffer);
   }
};

var detail = Details.pool.get( [] );
...

Properties

count Number

The current number of available objects, the default is 0, it will gradually increase with the recycle of the object, the maximum will not exceed the size specified when the constructor is called.

There are no properties that match your current filter settings. You can change your filter settings in the index section on this page. index

Methods

constructor
(
  • [cleanupFunc ]
  • size
)

Constructor for creating an object pool for the specific object type. You can pass a callback argument for process the cleanup logic when the object is recycled.

name type description
cleanupFunc optional Function

the callback method used to process the cleanup logic when the object is recycled.

size Number

initializes the length of the array

get
(
  • params
)
Object

Get and initialize an object from pool. This method defaults to null and requires the user to implement it.

name type description
params Any

parameters to used to initialize the object

returns:

type: Object

_get ( ) Object | Null

Get an object from pool, if no available object in the pool, null will be returned.

returns:

type: Object | Null

put ( )

Put an object into the pool.

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index