Quick display group question

Is this possible?

I want to limit a display group to 4 and only 4 objects. I want it to behave so that whenever I add a new object to the group, the oldest (first) object gets removed, keeping the number of children as a constant 4.

Is this easy to do, or is it a pipe dream?
[import]uid: 13529 topic_id: 8627 reply_id: 308627[/import]

I think it’s possible. You could probably just make a custom event listener that does the adding and removing and put in yourDisplayGroup:dispatchEvent whenever you need it done. This listener would involve using numObjects = yourDisplayGroup.numChildren to get the current number of objects in the display group. After that the logic shouldn’t be too tough.

If when you say “display group” do you mean the total of all display objects, then use display.getCurrentStage( ) to reference it. Otherwise, you’d obviously just use the name you chose to represent your group of display objects.

I hope that helps. [import]uid: 10211 topic_id: 8627 reply_id: 30993[/import]

Something like this maybe:

[code]
local group = display.newGroup()

function addToGroup( obj )
local numItems = group.numChildren
– print("num items: " … numItems)
if numItems < 4 then
group:insert(1, obj)
else
group:remove(4)
group:insert(1, obj)
end

print (“all my children”)
for i=1,group.numChildren do
print(group[i].myindex)
end

end

for i=1,10 do
local obj = display.newRect( 0, 0, 20, 20 )
obj.myindex = i
addToGroup(obj)
end
[/code] [import]uid: 8196 topic_id: 8627 reply_id: 31166[/import]

Thanks for the responses.

Tim, that’s definitely enough to get me going. Thanks!

Follow up question I should know by now: group:remove(4)…will that only remove the object from the group, or will it completely remove it (like obj:removeSelf())?
[import]uid: 13529 topic_id: 8627 reply_id: 31168[/import]