1. 程式人生 > >cocos 獲取所有子節點 -lua

cocos 獲取所有子節點 -lua

function UiTools:getAllChild(parent)
local node_list = {}
local children = parent:getChildren()

    local function seach_child(parente)
        local childCount = parente:getChildrenCount()
        if childCount < 1 then
            node_list[parente:getName()] = parente
        else 
            for i = 1, childCount do
                dump(parente)
                node_list[parente:getName()] = parente
                seach_child(parente:getChildren()[i])
            end
        end
    end 

for k , v in next ,children do 

    if #v:getChildren() < 1 then
        node_list[v:getName()] = v
        else
        seach_child(v)
    end
end
return node_list

end