Hi,
I’m positive I’ve done this before, but I’m having trouble clearing out group children.
Referencing the example code for group.numChildren and group:remove, here is my test code:
local rectGrp = display.newGroup() rectGrp.x = 52 rectGrp.y = 100 local function clearRects() for i=1, rectGrp.numChildren do print(i) rectGrp:remove(rectGrp[i]) end end local function renderRects(rects) for i=1, #rects do local rect = display.newRect(52\*(i-1), 0, 50, 100) rectGrp:insert(rect) end end renderRects({0,0,0,0,0}) timer.performWithDelay(2000, clearRects)
When the clearRects is run, only the children at odd indexes are being cleared (1,3,5). So children at index 2 and 4 are still displayed on screen.
The console outputs:
1 2 3 4 WARNING: main.lua:10: objectGroup:remove(): index of 4 out of range (should be 1 to 2) 5 WARNING: main.lua:10: objectGroup:remove(): index of 5 out of range (should be 1 to 2)
I’ve tried it with different amounts of display objects, and the result is the same. I’m quite baffled, and feel like I’m missing something obvious.
Removing the entire group with rectGrp:removeSelf() works as expected.
Results are the same on the Pubic and Daily build. Any help appreciated.
-dev