LuaExtend.lua 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. -- add empty ccslog function to avoid function not defined error.
  2. -- if you log something, add what you want in it.
  3. if cc.exports then
  4. cc.exports.ccslog = function(...) end
  5. else
  6. ccslog = function(...) end
  7. end
  8. -- used as metaTable
  9. local luaExtend = {}
  10. local function getChildInSubNodes(nodeTable, key)
  11. if #nodeTable == 0 then
  12. return nil
  13. end
  14. local child = nil
  15. local subNodeTable = {}
  16. for _, v in ipairs(nodeTable) do
  17. child = v:getChildByName(key)
  18. if (child) then
  19. return child
  20. end
  21. end
  22. for _, v in ipairs(nodeTable) do
  23. local subNodes = v:getChildren()
  24. if #subNodes ~= 0 then
  25. for _, v1 in ipairs(subNodes) do
  26. table.insert(subNodeTable, v1)
  27. end
  28. end
  29. end
  30. return getChildInSubNodes(subNodeTable, key)
  31. end
  32. luaExtend.__index = function(table, key)
  33. local root = table.root
  34. local child = root[key]
  35. if child then
  36. return child
  37. end
  38. child = root:getChildByName(key)
  39. if child then
  40. root[key] = child
  41. return child
  42. end
  43. child = getChildInSubNodes(root:getChildren(), key)
  44. if child then root[key] = child end
  45. return child
  46. end
  47. -- used as metaTable
  48. return luaExtend