Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Sprite:getNumChildren() — Gideros Forum

Sprite:getNumChildren()

Hello again!
I've ran into another problem and I'm pretty confused because of it. I apologize if this is an easy question.
In my game have a sprite to which I add other components.
Later on I need to remove them from the said sprite.

I tried something like this, but the index runs out of bounds. I did some research and found out that the sprite, even if destroyed still gets counted by "getNumChildren()". My question is why that is, and how should I manage removing sprites from their parents?
for i = 1, obstacles:getNumChildren() do
    local sprite = obstacles:getChildAt(i)
    if condition and sprite.body then
          self.world:destroyBody(sprite.body)
          obstacles:removeChild(sprite)
    end
end

Also ,the body and the sprite are removed visually when I destroy the body and the sprite using timers (using a different approach), but "getNumChildren()" continues to increment, therefore increasing the amount of iterations made and slowing my game down progressively.
Tagged:

Comments

Sign In or Register to comment.