You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB

  1. local g_effectCache = nil;
  2. local hited = 0;
  3. local called = 0;
  4. local missed = 0;
  5. -- 根据路径创建一个光效出来
  6. function createEffect(nodeFile)
  7. local effectNode = cc.StreamObject:loadFromFile(nodeFile);
  8. if effectNode == nil then
  9. print("创建光效失败,光效路径:" .. nodeFile);
  10. end
  11. return effectNode;
  12. end
  13. local createEffectImpl = createEffect;
  14. local removeing = false;
  15. -- 创建光效,并通过缓冲池来管理
  16. local function createEffectWithCache(filename, noCache)
  17. if noCache then
  18. -- 不使用缓存则直接创建光效
  19. return createEffectImpl(filename);
  20. else
  21. called = called + 1;
  22. local effect = g_effectCache:pop(filename);
  23. if effect then
  24. if effect:getParent() then
  25. local parent = effect:getParent();
  26. print("父亲不为空" , effect:getName() , parent , parent:getName() , type(parent) , tolua.type(parent) , parent.ClassName);
  27. error("父亲不为空");
  28. end
  29. print("复用光效" , filename , effect:getName());
  30. return effect;
  31. end
  32. missed = missed + 1;
  33. local effect = createEffectImpl(filename);
  34. -- 调试代码
  35. --effect:setName(effect:getName() .. filename .. tostring(index));
  36. -- 覆盖函数,让他可以回到池中
  37. function effect:removeFromParent()
  38. -- 被回收的光效,有可能已经被销毁了,这时候不能回收
  39. -- 或者被多次回收,就不再回收
  40. if tolua.isnull(self) or removeing then
  41. return
  42. end
  43. g_effectCache:push(filename , self);
  44. removeing = true;
  45. self:stop();
  46. cc.Node.removeFromParent(self);
  47. removeing = false;
  48. end
  49. return effect;
  50. end
  51. end
  52. -- 启用光效池,这个光效池每10秒清空一次超过10秒没使用的缓冲光效
  53. function startEffectCache()
  54. g_effectCache = cc.RefObjectCache:create();
  55. g_effectCache:start();
  56. createEffect = createEffectWithCache;
  57. end
  58. function clearEffectCache()
  59. if g_effectCache then
  60. g_effectCache:clear()
  61. end
  62. end
  63. function preloadEffectTemplate(effectFile)
  64. end