Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Removing all children from a Sprite — Gideros Forum

Removing all children from a Sprite

lyrenbrownlyrenbrown Member
edited January 2012 in Bugs and issues
I have this function:
function clear(sprite)
    for i = 1,sprite:getNumChildren() do
        sprite:removeChildAt(i)
    end
end
And I'm getting this error on the removeChildAt line:
main.lua:72: The supplied index is out of bounds.
stack traceback:
I didn't think that was possible. Are the child index values not always contiguous? If not, then how can I do this? I tried adding a guard but I get the same error:
function clear(sprite)
    for i = 1,sprite:getNumChildren() do
        if sprite:getChildAt(i) then
            sprite:removeChildAt(i)
        end
    end
end

Dislikes: jenzop

+1 -1 (+0 / -1 )Share on Facebook

Comments

  • Does reversing the "for" statement give you the same error? eg
    for i = #sprite:getNumChildren, 1, -1 do
    sprite:removeChildAt(i)
    end

    Likes: lyrenbrown

    +1 -1 (+1 / -0 )Share on Facebook
  • lyrenbrownlyrenbrown Member
    edited January 2012
    @Ghando Wow, thanks! That seems to fix it. Now I have this:
    function clear(sprite)
        for i = sprite:getNumChildren(), 1, -1 do
            sprite:removeChildAt(i)
        end
    end
    Why does that work?
  • Ah! So, the way I was doing it was forcing the whole array to be renumbered every iteration. That would also explain how the index values are always kept contiguous. Many thanks!
  • hnimhnim Member
    edited January 2012
    just change self:removeChildAt(i) to self:removeChildAt(1)
  • lyrenbrownlyrenbrown Member
    edited January 2012
    @hnim That's a good point, but I wonder if it's less efficient that way since the children will have to renumbered for every iteration? Seems O(n^2)'ish, no?
  • atilimatilim Maintainer
    edited January 2012
    @lyrenbrown, Children are stored as a hash table on Lua and as an ordered array on C++. Therefore, @hnim's solution is O(n) on Lua side and O(n^2) on C++ side. (But the time spent on C++ is negligible compared to Lua while removing a child)

    This is another solution (most probably the slowest one :) )
    function clear(sprite)
        while sprite:getNumChildren() > 0 do
            sprite:removeChildAt(1)
        end
    end
  • I assume that to remove the children of children you still have to do recursive removal?

    (I was confused in Objective C, because when releasing a view, it would then release child views that were retained by it, so I didn't have to do recursive removal, but it's different in this case?)
  • ndossndoss Guru
    edited January 2012
    I assume that to remove the children of children you still have to do recursive removal?
    If the deleted child is the only source of references to it's children, then I don't believe you have to recursively delete the children.

    Said another way ... if the children of the deleted child don't have any references to them, I believe they will be deleted by lua's garbage collector.

    Likes: atilim

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