removeSelf() on all displayObjects?

Hi there!

I have the following problem: I have a pause menu that allows you to exit back to the main menu of my game. The problem is that this needs to remove all displayobjects and timers (and several eventlisteners), obviously, to close the current screen, before opening the new screen.

This is a typical situation. However, I don’t know in which part of my game this function will be called, so it is hard for me to know which displayObjects actually exist at that moment, making it impossible to know which ones to remove.

Is there a way to know or see how many display objects exist on the stage? I’ve tried display.numChildren but this doesn’t work.

So basically what I need to be able to do is remove all displayobjects, without actually knowing how many exist at that time, nor what their names are.

Something likes this would be perfect but doesn’t work of course:

for i = 1, display.numChildren  
 display[i]:removeSelf()  
end  

This would work for other displayGroups, but not for the stage, alas.

Or are there other ways to do this? One thing I can think of is adding every displayObject i add to a special table as well, and going over the contents of this table to delete them, but that sounds very convoluted, to be honest. [import]uid: 70134 topic_id: 19686 reply_id: 319686[/import]

One thing I can think of is adding every displayObject i add to a special table

Thats the purpose of a displayGroup()

You create a displayGroup , (lets call it allMyStuff)

Every sprite, text, button, graphic which you add to the screen, you also insert into the displayGroup

In fact, calls such as newText take an optional first parameter of displayGroup to help you with this.

so:

local highScore = display.newText(allMyStuff,“the words”…etc
When everything lives in a single display group, to kill them all off, you can just use

display.remove(allMyStuff)

and the items plus their handlers are all deleted.

This is the premise on which the director class works: each screen is defined as a set of objects inside a display group.
Changing screens is literally the process of swapping between the display groups.

[import]uid: 108660 topic_id: 19686 reply_id: 76161[/import]

You *could* get the current number of active display objects with this:

local rootGroup = display.getCurrentStage()  
  
for i=rootGroup.numChildren, 1, -1 do  
 rootGroup[i]:removeSelf();  
end  

Although I think it’s a bit of a hack.
It’s best to keep track of your display objects yourself, either in a group or in your own lua tables. [import]uid: 70847 topic_id: 19686 reply_id: 76163[/import]

Cool, thanks.

Regarding putting everything in a displayGroup, doesn’t this come at a speed-penalty? That’s basically the reason why I don’t want to do this. [import]uid: 70134 topic_id: 19686 reply_id: 76165[/import]

Have you tried the storyboard.api. If your switching between scenes and having trouble removing objects and listeners this will work fine. I dont know about the speed penalties [import]uid: 75779 topic_id: 19686 reply_id: 76410[/import]

Hi Thomas,

If you get any kind of speed penalty at all it will almost certainly be unnoticeable. Groups are a very efficient way of handling this.

Peach :slight_smile: [import]uid: 52491 topic_id: 19686 reply_id: 76564[/import]