Scene removal not working

Objects from previous scenes while not visible, still exist and mess up future scenes. I can’t get this issue resolved. I have put storyboard.removeScene(“sceneName”) in every exitScene function as well as purgeScene. Also I have tried storyboard.purgeOnSceneChange = true, which is supposed to purge the scene when it changes but nothing is working for me. Does anyone have a solution? This is really messing up my game, causing it to freeze and crash [import]uid: 208811 topic_id: 34968 reply_id: 334968[/import]

  1. Might need to post some sample code
  2. Have you tried running garbage collection? This is necessary to completely wipe out nil objects (but has a perf penalty so shouldn’t be run constantly)
 collectgarbage( “collect” )

[

Here is one of my scenes, I’m posting it because it has the least code in it.
But it gives a representation of what I’m trying to do in other scenes. Where
in the code should I use collectgarbage( “collect” )



– maths_dif.lua


local storyboard = require( “storyboard” )
storyboard.purgeOnSceneChange = true
local scene = storyboard.newScene()

–storyboard.removeScene( “login” )


– BEGINNING OF YOUR IMPLEMENTATION

local image, image2
local easyButtonRelease = function( event )
storyboard.num1 = 10
storyboard.num2 = 30
storyboard.num4 = 11
storyboard.num5 = 100
storyboard.gotoScene( “scene1”, “slideLeft”, 1000 )
print(“touch”)
return true
end

local hardButtonRelease = function( event )
nums = {15, 20, 25, 50, 75, 100}
n1 = math.random(1,#nums)
n2 = math.random(1,#nums)
storyboard.num1 = (nums[n1])
storyboard.num2 = (nums[n2])
storyboard.num3 = 1
storyboard.num4 = 101
storyboard.num5 = 999
storyboard.num6 = 5
storyboard.num7 = 10
storyboard.gotoScene( “scene1”, “slideLeft”, 1000 )
print(“touch”)
return true

end

– Called when the scene’s view does not exist:
function scene:createScene( event )
local widget = require( “widget” )
local screenGroup = self.view

– local text = display.newText(“Please choose a difficulty”,15, 0, “comic sans ms”, 22)
– text:setTextColor(255, 250, 250)

image = display.newImage( “images/Background(640x480).png”, -20 ,0 )
t4 = display.newText(“Round 3: Hit the Target - Choose a difficulty” ,30, 25, “comic sans ms”, 20)
t4:setTextColor(255, 250, 250)
–screenGroup:insert( image )

–image.touch = onSceneTouch
–p1Button.touch = buttontouch
print( “\n2: createScene event” )

local easyButton = widget.newButton{
default = “images/Round3easy_button.png”,
over = “images/Round3easy_button.png”,
–onPress = button1Press,
onRelease = easyButtonRelease,
– fontSize = 20,
– labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
– label = “+”,
– emboss = true
}

–easyButton.touch = easyButtonRelease

local hardButton = widget.newButton{
default = “images/Round3hard_button.png”,
over = “images/Round3hard_button.png”,
–onPress = button1Press,
onRelease = hardButtonRelease,
– fontSize = 20,
– labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
– label = “+”,
– emboss = true
}

easyButton.x = 240; easyButton.y = 140
hardButton.x = 240; hardButton.y = 230
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )

print( “2: enterScene event” )
– image = display.newImage( “Background(480x320).png”, 0 ,0 )
– t4 = display.newText(“Round 3: Hit the Target - Choose a difficulty” ,30, 25, “comic sans ms”, 20)
– t4:setTextColor(0, 0, 0)
– remove previous scene’s view
storyboard.removeScene(“player”)
display.remove(screenGroup)

– -- Update Lua memory text display
– local showMem = function()
image:addEventListener( “touch”, image )
–easyButton:addEventListener( “touch”, easyButton )
– text3.isVisible = true
– text2.text = text2.text … collectgarbage(“count”)/1000 … “MB”
– text2.x = display.contentWidth * 0.5
– end
– memTimer = timer.performWithDelay( 1000, showMem, 1 )
end
– Called when scene is about to move offscreen:
function scene:exitScene()

print( “2: exitScene event” )

– remove touch listener for image
storyboard.removeScene(“maths_dif”)
image:removeEventListener( “touch”, image )
–easyButton.isVisible = false

end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )

print( “((destroying scene 2’s view))” )
end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene
[import]uid: 208811 topic_id: 34968 reply_id: 139004[/import]

  1. Please encapsulate your code in the html tags < code > < / code> (without spaces) so that it’s easy to read. This makes it much more likely for people to respond to your questions. :slight_smile:

  2. nums should be set to local, currently you’re declaring it as a scene-wide global. Same as n1, n2

  3. You should move the require statement for widget to the beginning of the file or thereabouts. Not really sure what happens if you keep trying to require it in on every createScene…

  4. You’re creating the text object “t4” but not inserting it into screenGroup. So t4 is never being deleted. Same for image. Destroying a scene will only destroy the objects that are a part of the scene’s hierarchy.

  5. You’re calling removeScene() and display.remove, neither of which should be done.
    a. You’re trying to remove screenGroup, which is out of scope, so it’s nil. No point
    b. You should never try to remove it anyway, that’s what purge is for
    c. The difference between purgeScene and removeScene:

purgeScene:

  • kills .view (screenGroup, in your code)
  • if you return to the scene, it calls createScene() first

removeScene

  • kills .view
  • it has no magical control over lua so any code you had in the file prior to createScene still exists.
  • if you return to the scene, it calls everything in the scene BEFORE createScene first. That means re-running any code prior [import]uid: 41884 topic_id: 34968 reply_id: 139009[/import]

Although this may not be the best solution, I use the removeAll at the top of each scene which works for me. I don’t care too much about each scene being recreated. This was the best solution for me because I needed to remove the widget controls such as a textField and this worked for me.

local storyboard = require( "storyboard" )  
storyboard.removeAll()  

Warren
[import]uid: 184193 topic_id: 34968 reply_id: 139058[/import]

  1. Might need to post some sample code
  2. Have you tried running garbage collection? This is necessary to completely wipe out nil objects (but has a perf penalty so shouldn’t be run constantly)
 collectgarbage( “collect” )

[

Here is one of my scenes, I’m posting it because it has the least code in it.
But it gives a representation of what I’m trying to do in other scenes. Where
in the code should I use collectgarbage( “collect” )



– maths_dif.lua


local storyboard = require( “storyboard” )
storyboard.purgeOnSceneChange = true
local scene = storyboard.newScene()

–storyboard.removeScene( “login” )


– BEGINNING OF YOUR IMPLEMENTATION

local image, image2
local easyButtonRelease = function( event )
storyboard.num1 = 10
storyboard.num2 = 30
storyboard.num4 = 11
storyboard.num5 = 100
storyboard.gotoScene( “scene1”, “slideLeft”, 1000 )
print(“touch”)
return true
end

local hardButtonRelease = function( event )
nums = {15, 20, 25, 50, 75, 100}
n1 = math.random(1,#nums)
n2 = math.random(1,#nums)
storyboard.num1 = (nums[n1])
storyboard.num2 = (nums[n2])
storyboard.num3 = 1
storyboard.num4 = 101
storyboard.num5 = 999
storyboard.num6 = 5
storyboard.num7 = 10
storyboard.gotoScene( “scene1”, “slideLeft”, 1000 )
print(“touch”)
return true

end

– Called when the scene’s view does not exist:
function scene:createScene( event )
local widget = require( “widget” )
local screenGroup = self.view

– local text = display.newText(“Please choose a difficulty”,15, 0, “comic sans ms”, 22)
– text:setTextColor(255, 250, 250)

image = display.newImage( “images/Background(640x480).png”, -20 ,0 )
t4 = display.newText(“Round 3: Hit the Target - Choose a difficulty” ,30, 25, “comic sans ms”, 20)
t4:setTextColor(255, 250, 250)
–screenGroup:insert( image )

–image.touch = onSceneTouch
–p1Button.touch = buttontouch
print( “\n2: createScene event” )

local easyButton = widget.newButton{
default = “images/Round3easy_button.png”,
over = “images/Round3easy_button.png”,
–onPress = button1Press,
onRelease = easyButtonRelease,
– fontSize = 20,
– labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
– label = “+”,
– emboss = true
}

–easyButton.touch = easyButtonRelease

local hardButton = widget.newButton{
default = “images/Round3hard_button.png”,
over = “images/Round3hard_button.png”,
–onPress = button1Press,
onRelease = hardButtonRelease,
– fontSize = 20,
– labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
– label = “+”,
– emboss = true
}

easyButton.x = 240; easyButton.y = 140
hardButton.x = 240; hardButton.y = 230
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )

print( “2: enterScene event” )
– image = display.newImage( “Background(480x320).png”, 0 ,0 )
– t4 = display.newText(“Round 3: Hit the Target - Choose a difficulty” ,30, 25, “comic sans ms”, 20)
– t4:setTextColor(0, 0, 0)
– remove previous scene’s view
storyboard.removeScene(“player”)
display.remove(screenGroup)

– -- Update Lua memory text display
– local showMem = function()
image:addEventListener( “touch”, image )
–easyButton:addEventListener( “touch”, easyButton )
– text3.isVisible = true
– text2.text = text2.text … collectgarbage(“count”)/1000 … “MB”
– text2.x = display.contentWidth * 0.5
– end
– memTimer = timer.performWithDelay( 1000, showMem, 1 )
end
– Called when scene is about to move offscreen:
function scene:exitScene()

print( “2: exitScene event” )

– remove touch listener for image
storyboard.removeScene(“maths_dif”)
image:removeEventListener( “touch”, image )
–easyButton.isVisible = false

end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )

print( “((destroying scene 2’s view))” )
end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene
[import]uid: 208811 topic_id: 34968 reply_id: 139004[/import]

  1. Please encapsulate your code in the html tags < code > < / code> (without spaces) so that it’s easy to read. This makes it much more likely for people to respond to your questions. :slight_smile:

  2. nums should be set to local, currently you’re declaring it as a scene-wide global. Same as n1, n2

  3. You should move the require statement for widget to the beginning of the file or thereabouts. Not really sure what happens if you keep trying to require it in on every createScene…

  4. You’re creating the text object “t4” but not inserting it into screenGroup. So t4 is never being deleted. Same for image. Destroying a scene will only destroy the objects that are a part of the scene’s hierarchy.

  5. You’re calling removeScene() and display.remove, neither of which should be done.
    a. You’re trying to remove screenGroup, which is out of scope, so it’s nil. No point
    b. You should never try to remove it anyway, that’s what purge is for
    c. The difference between purgeScene and removeScene:

purgeScene:

  • kills .view (screenGroup, in your code)
  • if you return to the scene, it calls createScene() first

removeScene

  • kills .view
  • it has no magical control over lua so any code you had in the file prior to createScene still exists.
  • if you return to the scene, it calls everything in the scene BEFORE createScene first. That means re-running any code prior [import]uid: 41884 topic_id: 34968 reply_id: 139009[/import]

Although this may not be the best solution, I use the removeAll at the top of each scene which works for me. I don’t care too much about each scene being recreated. This was the best solution for me because I needed to remove the widget controls such as a textField and this worked for me.

local storyboard = require( "storyboard" )  
storyboard.removeAll()  

Warren
[import]uid: 184193 topic_id: 34968 reply_id: 139058[/import]