What am I doing wrong?

To change scenes using Widget and Director…

 

local function toScreen2(event) if event.phase == "ended" then display.remove(background);background=nil -- All other objects are removed. display.remove(button);button=nil -- Some how this stays eating up memory. end return true end local button = widget.newButton { width = 400, height = 75, label = "Screen1", labelColor = { default = {51,181,229,255}, over = {0, 255, 127,255} }, font = native.systemFont, fontSize = 30, onRelease = toScreen2, } button.x = display.contentCenterX button.y = 125 Group:insert(button)

In my " toScreen2" function, all objects are removed except the actual button. How do I make sure the pressed button is removed with the rest of the objects on the screen? I tried to declare my variables beforehand but to no avail. What am I doing wrong?

I’m not too familiar with Director, but why not remove the “Group” instead of each object individually?  This is essentially how storyboard works.

Using removeSelf() on display group will trigger removeSelf() on all it’s childs automatically.

However, if you also have reference to object, like ‘background’, then you also have to nil it. RemoveSelf only destroys only visual part of display object, some data still persists if referenced.

Button isn’t being removed because you haven’t pre-declared it above the function. Try removing the local from in-front of “button” and then put “local button” above the toScreen2 function. 

TandG is correct, write it like this:

local button = nil local function toScreen2(event) if event.phase == "ended" then display.remove(background);background=nil -- All other objects are removed. display.remove(button);button=nil -- Some how this stays eating up memory. end return true end button = widget.newButton { width = 400, height = 75, label = "Screen1", labelColor = { default = {51,181,229,255}, over = {0, 255, 127,255} }, font = native.systemFont, fontSize = 30, onRelease = toScreen2, } button.x = display.contentCenterX button.y = 125 Group:insert(button)

I’m not too familiar with Director, but why not remove the “Group” instead of each object individually?  This is essentially how storyboard works.

Using removeSelf() on display group will trigger removeSelf() on all it’s childs automatically.

However, if you also have reference to object, like ‘background’, then you also have to nil it. RemoveSelf only destroys only visual part of display object, some data still persists if referenced.

Button isn’t being removed because you haven’t pre-declared it above the function. Try removing the local from in-front of “button” and then put “local button” above the toScreen2 function. 

TandG is correct, write it like this:

local button = nil local function toScreen2(event) if event.phase == "ended" then display.remove(background);background=nil -- All other objects are removed. display.remove(button);button=nil -- Some how this stays eating up memory. end return true end button = widget.newButton { width = 400, height = 75, label = "Screen1", labelColor = { default = {51,181,229,255}, over = {0, 255, 127,255} }, font = native.systemFont, fontSize = 30, onRelease = toScreen2, } button.x = display.contentCenterX button.y = 125 Group:insert(button)