cocos2dx 对象池 Lua

local NianGaoNode = requirePack("task.toyboxScripts.NianGaoNode")

local NianGaoNodePool = class("NianGaoNodePool")

function NianGaoNodePool:ctor()

    self.nodePoll ={};

    print(" 20 20 20 对象池的 构造 ")

end

function NianGaoNodePool:getNianGao() 

    if( #self.nodePoll > 0 ) then 

        local item = table.remove (self.nodePoll,1) --返回table数组部分位于pos位置的元素. 其后的元素会被前移. pos参数可选, 默认为table长度, 即从最后一个元素删起

        print("10 獲取到舊的 前 " ,#self.nodePoll, item:getReferenceCount() )

        --item:release();

       -- print("10 獲取到舊的 後 " , item:getReferenceCount() )

        return item ;

    else 

        local item = NianGaoNode.new();

        return item ; 

    end 

end

 

function NianGaoNodePool:huishou(newNode)

       print("22 remove 前 ", newNode:getReferenceCount())

 newNode:removeFromParent();

print("22 remove 後 ", newNode:getReferenceCount())

       newNode:retain();

       print("22 retain 後 ", newNode:getReferenceCount())

       if(newNode:getReferenceCount() > 2 ) then 

           newNode:release();

           print("22 release 後 ", newNode:getReferenceCount())

       end

       table.insert(self.nodePoll,newNode) ; --table的数组部分指定位置(pos)插入值为value的一个元素. pos参数可选, 默认为数组部分末尾.

       print("对象池的 回收之后",#self.nodePoll)

     end

 

function NianGaoNodePool:clearAll()

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

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

    for k,v in ipairs(self.nodePoll) do

       v:removeFromParent();

    end   

    self.nodePoll = {}   

end

 

return NianGaoNodePool

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值