Creator 对象池的用法 享元模式

const { ccclass, property } = cc._decorator;

import YBConst, { PopupLayerZorder } from '../core/constant/YBConst';

import YBLog from '../core/utils/YBLog';

/**

 * 箭的尾巴的对象池。

 */

@ccclass

export default class ArrowTailPool extends cc.Component {

    // LIFE-CYCLE CALLBACKS:

    enemyPool: cc.NodePool;

    onLoad() {

        // YBLog.log("NodeBgPool", "25 25 25 对象池 ");

        this.enemyPool = new cc.NodePool();

    };

    public getparticleTail(callback): any {

        if (this.enemyPool.size() > 0) {

            let newNode = this.enemyPool.get();

            callback(newNode);

        } else {

            //YBLog.log("NodeBgPool", "新建对象");

            // 如果没有空闲对象,我们就用 cc.instantiate 重新创建

            cc.resources.load("dynamicRes/prefab/tail", cc.Prefab, function (err, prefab) {

                if (err) {

                    cc.error(err);

                    return null;

                }

                let newNode = cc.instantiate(prefab);

                this.enemyPool.put(newNode); // 通过 put 接口放入对象池

                callback(this.enemyPool.get());

            }.bind(this));

        }

    }

    //回收 

    public huishou(newNode: any) {

        if (newNode) {

            this.enemyPool.put(newNode); // 通过 put 接口放入对象池

          //  YBLog.log( "ArrowTailPool","回收之后:",this.enemyPool.size() );

        }

    }

    //回收 

    public clearAll() {

        this.enemyPool.clear(); // 通过 put 接口放入对象池

        // YBLog.log( "NodeBgPool","清理之后之后:",this.enemyPool.size() );

    }

    start() {

      //  YBLog.log("NodeBgPool", " 初始化 ");

    }

    // update (dt) {}

}

猜你喜欢

转载自blog.csdn.net/ting100/article/details/108754995