标签:返回 turn 相对 参考 The == cos man scripting
版本:2.3.4
参考:
cocos的NodePool提供了一个数组,保存和获取数组内对象,并在保存和获取时可执行对象上的unuse和reuse方法。
使用相对简单,看看cocos教程即可。
主要是在获取对象池中实例时,不存在实例,则返回null,不会自动新建。 新建的代码得自己写。
cocos对象池源码
CCNodePool.js:
cc.NodePool = function (poolHandlerComp) {
this.poolHandlerComp = poolHandlerComp;
this._pool = [];
};
cc.NodePool.prototype = {
constructor: cc.NodePool,
size: function () {
return this._pool.length;
},
clear: function () {
var count = this._pool.length;
for (var i = 0; i < count; ++i) {
this._pool[i].destroy();
}
this._pool.length = 0;
},
put: function (obj) {
if (obj && this._pool.indexOf(obj) === -1) {
// Remove from parent, but don‘t cleanup
obj.removeFromParent(false);
// Invoke pool handler
var handler = this.poolHandlerComp ? obj.getComponent(this.poolHandlerComp) : null;
if (handler && handler.unuse) {
handler.unuse();
}
this._pool.push(obj);
}
},
get: function () {
var last = this._pool.length-1;
if (last < 0) {
return null;
}
else {
// Pop the last object in pool
var obj = this._pool[last];
this._pool.length = last;
// Invoke pool handler
var handler = this.poolHandlerComp ? obj.getComponent(this.poolHandlerComp) : null;
if (handler && handler.reuse) {
handler.reuse.apply(handler, arguments);
}
return obj;
}
}
};
module.exports = cc.NodePool;
标签:返回 turn 相对 参考 The == cos man scripting
原文地址:https://www.cnblogs.com/gamedaybyday/p/13092339.html