Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Snippet: Count total children of a parent — Gideros Forum

Snippet: Count total children of a parent

MikeHartMikeHart Guru
edited January 2012 in Code snippets
Hi folks,

as I converting a game to gideros, I need to know how many children a parent has, including the children of a the children.

I came up with this function
local getTotalChildNum = nil
getTotalChildNum = function(parent)
	local nc = 0
	for index = 1,parent:getNumChildren() do
		nc = nc + 1
		local child = parent:getChildAt(index)
		nc = nc + getTotalChildNum(child)
	end
	return nc
end
To use it, just call it like this:
print ("Total children in stage: ".. getTotalChildNum(stage))

Likes: gorkem, oleg

+1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.