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.
Comments
do so:
for i = obstacles:getNumChildren() , 1,-1 do
or so:
for i = 1, obstacles:getNumChildren() do
obstacles:removeChildAt(1)
end
Likes: SinisterSoft, antix, Apollo14, MoKaLux
https://play.google.com/store/apps/developer?id=razorback456
мій блог по гідерос https://simartinfo.blogspot.com
Слава Україні!