Reuse of display groups between levels

Hi

In the start of my main file I declare my display groups (background,main and more)

local gameDisplay = display.newGroup()

local bgDisplay = display.newGroup()

local mainDisplay = display.newGroup()

local frontDisplay = display.newGroup()

local textDisplay = display.newGroup()

local ballGroup = display.newGroup()

I also insert all my display groups into one group using a dedicated function
 

function setupDisplay()

                gameDisplay:insert(bgDisplay);

                gameDisplay:insert(mainDisplay);

                gameDisplay:insert(ballGroup);

                gameDisplay:insert(frontDisplay);

                gameDisplay:insert(textDisplay);

                frontDisplay:toFront();

end

what will be the best way to clear and reuses the display groups between levels (or re-play a level)?

My motivation is to clear the memory for better performance

I’m trying to use the below function before placing the new display objects in the new level but it’s not working so good (simulator present dark display without printing errors)

function clearGameDisplay ()

                display.remove(bgDisplay)

                bgDisplay.numChildren = 0

                display.remove(mainDisplay)

                mainDisplay.numChildren = 0

                display.remove(ballGroup)

                ballGroup.numChildren = 0

                display.remove(frontDisplay)

                frontDisplay.numChildren = 0

                display.remove(textDisplay)

                textDisplay.numChildren = 0             

                bgDisplay = display.newGroup()

                mainDisplay = display.newGroup()

                frontDisplay = display.newGroup()

                textDisplay = display.newGroup()

                ballGroup = display.newGroup()

end

Thanks

Guy

 

Hi Guy,

I’ll write a few points here:

  1. the “numChildren” command is for reading the number of children in a group. You can’t use it to just clear out a group by setting it to 0.

  2. if you want to keep using these groups, don’t declare them again in your “clear” function.

  3. one of the most common ways to clear out a group (while keeping it around) is to reverse-iterate loop through the number of children, clearing each element in the loop:

[lua]

for i=myGroup.numChildren,1,-1 do

   display.remove( myGroup[i] ) ; myGroup[i] = nil

end

[/lua]

Hope this helps,

Brent

Hi Brent,

Thanks for your replay

Is this the best way to clear the memory/resources between the game levels?

I plan to change between the game levels the display objects or the display objects locations.

Do you think it’s better to declare the display groups in the start of each level function and then to remove them (removeself&nil) in the end?

 

Regards,

Guy  

 

Hi Guy,

It really depends on your app. If you’re going to be using the same basic display groups from scene to scene, it’s probably better to just declare them once. If the scenes will change (in design and layout), then you should probably clear the groups and make new ones that are appropriate to the scene.

You should almost always clear out group children from scene to scene, unless you are going to use the exact same objects in every scene.

Best regards,

Brent

Thank you Brent

:slight_smile:

regards,

Guy

Hi Guy,

I’ll write a few points here:

  1. the “numChildren” command is for reading the number of children in a group. You can’t use it to just clear out a group by setting it to 0.

  2. if you want to keep using these groups, don’t declare them again in your “clear” function.

  3. one of the most common ways to clear out a group (while keeping it around) is to reverse-iterate loop through the number of children, clearing each element in the loop:

[lua]

for i=myGroup.numChildren,1,-1 do

   display.remove( myGroup[i] ) ; myGroup[i] = nil

end

[/lua]

Hope this helps,

Brent

Hi Brent,

Thanks for your replay

Is this the best way to clear the memory/resources between the game levels?

I plan to change between the game levels the display objects or the display objects locations.

Do you think it’s better to declare the display groups in the start of each level function and then to remove them (removeself&nil) in the end?

 

Regards,

Guy  

 

Hi Guy,

It really depends on your app. If you’re going to be using the same basic display groups from scene to scene, it’s probably better to just declare them once. If the scenes will change (in design and layout), then you should probably clear the groups and make new ones that are appropriate to the scene.

You should almost always clear out group children from scene to scene, unless you are going to use the exact same objects in every scene.

Best regards,

Brent

Thank you Brent

:slight_smile:

regards,

Guy