码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos Creator 对象池NodePool

时间:2020-06-11 13:40:06      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:返回   turn   相对   参考   The   ==   cos   man   scripting   

版本:2.3.4

参考:

cocos教程:使用对象池

 

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;

  

 

Cocos Creator 对象池NodePool

标签:返回   turn   相对   参考   The   ==   cos   man   scripting   

原文地址:https://www.cnblogs.com/gamedaybyday/p/13092339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!