local g_effectCache = nil; local hited = 0; local called = 0; local missed = 0; -- 根据路径创建一个光效出来 function createEffect(nodeFile) local effectNode = cc.StreamObject:loadFromFile(nodeFile); if effectNode == nil then print("创建光效失败,光效路径:" .. nodeFile); end return effectNode; end local createEffectImpl = createEffect; local removeing = false; -- 创建光效,并通过缓冲池来管理 local function createEffectWithCache(filename, noCache) if noCache then -- 不使用缓存则直接创建光效 return createEffectImpl(filename); else called = called + 1; local effect = g_effectCache:pop(filename); if effect then if effect:getParent() then local parent = effect:getParent(); print("父亲不为空" , effect:getName() , parent , parent:getName() , type(parent) , tolua.type(parent) , parent.ClassName); error("父亲不为空"); end print("复用光效" , filename , effect:getName()); return effect; end missed = missed + 1; local effect = createEffectImpl(filename); -- 调试代码 --effect:setName(effect:getName() .. filename .. tostring(index)); -- 覆盖函数,让他可以回到池中 function effect:removeFromParent() -- 被回收的光效,有可能已经被销毁了,这时候不能回收 -- 或者被多次回收,就不再回收 if tolua.isnull(self) or removeing then return end g_effectCache:push(filename , self); removeing = true; self:stop(); cc.Node.removeFromParent(self); removeing = false; end return effect; end end -- 启用光效池,这个光效池每10秒清空一次超过10秒没使用的缓冲光效 function startEffectCache() g_effectCache = cc.RefObjectCache:create(); g_effectCache:start(); createEffect = createEffectWithCache; end function clearEffectCache() if g_effectCache then g_effectCache:clear() end end function preloadEffectTemplate(effectFile) end