NodePool Class
Module: cc
cc.NodePool is the cache pool designed for node type.
It can helps you to improve your game performance for objects which need frequent release and recreate operations
It's recommended to create cc.NodePool instances by node type, the type corresponds to node type in game design, not the class,
for example, a prefab is a specific node type.
When you create a node pool, you can pass a Component which contains unuse, reuse functions to control the content of node.
Some common use case is :
1. Bullets in game (die very soon, massive creation and recreation, no side effect on other objects)<br/>
2. Blocks in candy crash (massive creation and recreation)<br/>
etc...
Index
Properties
poolHandlerCompFunction|StringThe pool handler component, it could be the class name or the constructor.
Methods
constructorConstructor for creating a pool for a specific node template (usually a prefab).sizeThe current available size in the poolclearDestroy all cached nodes in the poolputPut a new Node into the pool.getGet a obj from pool, if no available object in pool, null will be returned.
Details
Properties
poolHandlerComp
The pool handler component, it could be the class name or the constructor.
| meta | description |
|---|---|
| Type | Function | String |
| Defined in | extensions/ccpool/CCNodePool.js:76 |
Methods
constructor
Constructor for creating a pool for a specific node template (usually a prefab). You can pass a component (type or name) argument for handling event for reusing and recycling node.
| meta | description |
|---|---|
| Defined in | extensions/ccpool/CCNodePool.js:57 |
Parameters
poolHandlerCompFunction | String !#en The constructor or the class name of the component to control the unuse/reuse logic. !#zh 处理节点回收和复用事件逻辑的组件类型或名称。
Examples
properties: {
template: cc.Prefab
},
onLoad () {
// MyTemplateHandler is a component with 'unuse' and 'reuse' to handle events when node is reused or recycled.
this.myPool = new cc.NodePool('MyTemplateHandler');
}
size
The current available size in the pool
| meta | description |
|---|---|
| Returns | Number |
| Defined in | extensions/ccpool/CCNodePool.js:88 |
clear
Destroy all cached nodes in the pool
| meta | description |
|---|---|
| Defined in | extensions/ccpool/CCNodePool.js:98 |
put
Put a new Node into the pool. It will automatically remove the node from its parent without cleanup. It will also invoke unuse method of the poolHandlerComp if exist.
| meta | description |
|---|---|
| Defined in | extensions/ccpool/CCNodePool.js:111 |
Parameters
objNode
Examples
let myNode = cc.instantiate(this.template);
this.myPool.put(myNode);
get
Get a obj from pool, if no available object in pool, null will be returned. This function will invoke the reuse function of poolHandlerComp if exist.
| meta | description |
|---|---|
| Returns | Node | Null |
| Defined in | extensions/ccpool/CCNodePool.js:139 |
Parameters
paramsAny !#en Params to pass to 'reuse' method in poolHandlerComp !#zh 向 poolHandlerComp 中的 'reuse' 函数传递的参数
Examples
let newNode = this.myPool.get();