display.remove confusion

hey guys

i got lot of buttons in my app

so i wanna remove them and make them nil whenever i wanna exit the scene…

and all these buttons are inserted in sceneGroup…

so should i remove these buttons by :

display.remove(button1)

display.remove(button2)

display.remove(button3)

display.remove(button4)

display.remove(button5)

button1 = nil

button2 = nil 

button3 = nil 

button4 = nil 

button5 = nil

etc…

or should i use:

display.remove(sceneGroup)

sceneGroup = nil ???

plz help me

You will need to set the variables for each button to nil in order to remove the buttons from memory, so your first code block is better.

An improvement on that is to update each removal to:

button1 = display.remove( button1 )

because .remove() doesn’t return anything.

Going a step further, if you put all the buttons into a display group and reference them on that group, you won’t have to remove them individually and you will be able to use your second code block instead. This is because, to properly remove a display object from memory, you need to remove the display component (the image) and the variable reference.

So, if you create a group, then create the buttons and make them attached to that group, you can remove it and everything under it at once. Having separate, globally visible variables for display objects stops you from doing that.

Eg:

-- create the buttons and their parent group local btnGroup = display.newGroup() btnGroup.btn1 = display.newImage( "button1.png" ) btnGroup.btn2 = display.newImage( "button2.png" ) -- remove the button group and all the buttons in it btnGroup = display.remove( btnGroup )

Also if these are added to the scene’s group then composer will handle disposing the buttons automatically.  You don’t need to remove them when exiting the scene.

You will need to set the variables for each button to nil in order to remove the buttons from memory, so your first code block is better.

An improvement on that is to update each removal to:

button1 = display.remove( button1 )

because .remove() doesn’t return anything.

Going a step further, if you put all the buttons into a display group and reference them on that group, you won’t have to remove them individually and you will be able to use your second code block instead. This is because, to properly remove a display object from memory, you need to remove the display component (the image) and the variable reference.

So, if you create a group, then create the buttons and make them attached to that group, you can remove it and everything under it at once. Having separate, globally visible variables for display objects stops you from doing that.

Eg:

-- create the buttons and their parent group local btnGroup = display.newGroup() btnGroup.btn1 = display.newImage( "button1.png" ) btnGroup.btn2 = display.newImage( "button2.png" ) -- remove the button group and all the buttons in it btnGroup = display.remove( btnGroup )

Also if these are added to the scene’s group then composer will handle disposing the buttons automatically.  You don’t need to remove them when exiting the scene.