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) {}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值