composer remove a scene properly & remove variable (not an object)

Hi everyone  :D 


Update :

How to remove a variable (/reset its value) ? Just a variable not an object :slight_smile:

The code of variable :

[lua]

local prevScene = composer.getSceneName( “previous” )

[/lua]

Because it store the same value (function back to last page show the first page you call it from - no matter you call from which page, when it suppose to go to previous page recently)

<< I tried to set it to nil - prompt error

<< tried to remove it with removeSelf(), not working too.

Any ideas? Please help  ^_^ 

Thanks  :wink:

the scene:destroy function is called if you really destroy the scene (for example call composer.removeHidden() from another scene). If you just leave a scene scene:hide() is called.

In your code you expect it to be the other way around and call composer.removeScene from within the function scene:destroy() which runs after removeScene is called.

Maybe your variable question is answered then as well.

I think I understand ! Thank you so much for clarifying  :smiley:

Continue coding>>>

btw, I still troubled with variable… set it to be nil in function scene:hide() and next time calling fail… looks like it didn’t reload the variable  :frowning:

Bumping the post.

Still struggling in coding…

Anyone? Remove the variable or reset its value… 

To reset a variable:

someVariable = nil

Rob

Did work for only first time call, but after go to next scene and calling the function again, error :

“Runtime error, bad argument #1 to ‘find’ (string expected )”

Maybe because this line under function scene:hide  ?

[lua]

function scene:hide(event)

if event.phase ==“did” then

composer.removeScene(“current”)

print ("remove current scene ")

prevScene = nil

end

end

[/lua]

above is the code for the scene calling from menu bar (which can be call from other scene by clicking the menu and this button)

Anything wrong?

You’re going to need to provide more information.  More relevant code?  What line number is the error happening at?  Is there more to the error message?

http://docs.coronalabs.com/guide/basics/debugging/index.html

The code for the scene been calling out when click the button from menu

[lua]

local prevScene = composer.getSceneName( “previous” )

local function goBack(event)

if event.phase == “ended” then

composer.gotoScene(prevScene)

print (“go to scene”,prevScene)

end

return true

end

function scene:create(event)

a button to call onEvent = goBack()

end

function scene:show(event)

end

function scene:hide(event)

if event.phase ==“did” then

composer.removeScene(“current”)

print ("remove current scene ")

prevScene = nil

end

end

function scene:destroy(event)

end

scene:addEventListener(“create”,scene)

scene:addEventListener(“show”,scene)

scene:addEventListener(“hide”,scene)

scene:addEventListener(“destroy”,scene)

return scene

[/lua]

corona error :

go to the scene >>

2a962vb.jpg

>> move to another scene and calling that scene again >> 

2qki4u9.jpg

Just for the record, anytime I transition to a new scene I find it useful to just completely clean up the last. I do this by using:

 local sceneName = composer.getSceneName( "previous" ) if sceneName ~= nil then composer.removeScene( sceneName, false) end

it’s because you’re assigning the value to the variable outside of any function, so that code will run only once when the scene in loaded - because by default the scene code itself is not unloaded/reloaded during a scene change, so simply *displaying* a previously loaded scene for a second/third/etc time will NOT cause that code to reload/re-execute.

you can fix this by recycling old scenes to unload their code, forcing a reload on next display.  (as others have already mentioned)

or you can fix it without recycling by getting into the habit of not *assigning* values at module scope.  it’s ok to *declare* those free-floating locals at the top of your module, but assign their values in either create (if it needs to happen just once) or in show (if it needs to happen each time scene displayed).  then clean up correspondingly in either destroy or hide.

OMG thank you sooooo much!! I just add a line of ‘composer.recycleOnSceneChange = true’ and it fix! how amazing :slight_smile:

btw, could I ask you another question (Great explanation and I really understand the theory :smiley: )

My overlay scene still passing touches to scene behind, even in the showOverlay function I had set ‘isModal = true’

the code :

[lua]

function closeSideBar(event)

–closeSideBar 

end

function iconListener1( event )

–code of tutorial building a sliding menu

    if ( event.phase == “moved” ) then

        local dx = math.abs(event.x - event.xStart ) 

        if ( dx > 5 ) then

            scrollView:takeFocus( event ) 

        end

– added my code

local options = {

effect = “fade”,

time = 100,

isModal = true

}

elseif ( event.phase == “ended” ) then

composer.showOverlay(“a”,options)

–original tutorial code

timer.performWithDelay( 10, function() 

scrollView:removeSelf(); scrollView = nil; end )

return true

end

return true

end

– continue with other function of other button in sidebar menu

–the sidebar menu code-from the same tutorial

[/lua]

Thanks :smiley:

the scene:destroy function is called if you really destroy the scene (for example call composer.removeHidden() from another scene). If you just leave a scene scene:hide() is called.

In your code you expect it to be the other way around and call composer.removeScene from within the function scene:destroy() which runs after removeScene is called.

Maybe your variable question is answered then as well.

I think I understand ! Thank you so much for clarifying  :smiley:

Continue coding>>>

btw, I still troubled with variable… set it to be nil in function scene:hide() and next time calling fail… looks like it didn’t reload the variable  :frowning:

Bumping the post.

Still struggling in coding…

Anyone? Remove the variable or reset its value… 

To reset a variable:

someVariable = nil

Rob

Did work for only first time call, but after go to next scene and calling the function again, error :

“Runtime error, bad argument #1 to ‘find’ (string expected )”

Maybe because this line under function scene:hide  ?

[lua]

function scene:hide(event)

if event.phase ==“did” then

composer.removeScene(“current”)

print ("remove current scene ")

prevScene = nil

end

end

[/lua]

above is the code for the scene calling from menu bar (which can be call from other scene by clicking the menu and this button)

Anything wrong?

You’re going to need to provide more information.  More relevant code?  What line number is the error happening at?  Is there more to the error message?

http://docs.coronalabs.com/guide/basics/debugging/index.html

The code for the scene been calling out when click the button from menu

[lua]

local prevScene = composer.getSceneName( “previous” )

local function goBack(event)

if event.phase == “ended” then

composer.gotoScene(prevScene)

print (“go to scene”,prevScene)

end

return true

end

function scene:create(event)

a button to call onEvent = goBack()

end

function scene:show(event)

end

function scene:hide(event)

if event.phase ==“did” then

composer.removeScene(“current”)

print ("remove current scene ")

prevScene = nil

end

end

function scene:destroy(event)

end

scene:addEventListener(“create”,scene)

scene:addEventListener(“show”,scene)

scene:addEventListener(“hide”,scene)

scene:addEventListener(“destroy”,scene)

return scene

[/lua]

corona error :

go to the scene >>

2a962vb.jpg

>> move to another scene and calling that scene again >> 

2qki4u9.jpg

Just for the record, anytime I transition to a new scene I find it useful to just completely clean up the last. I do this by using:

 local sceneName = composer.getSceneName( "previous" ) if sceneName ~= nil then composer.removeScene( sceneName, false) end

it’s because you’re assigning the value to the variable outside of any function, so that code will run only once when the scene in loaded - because by default the scene code itself is not unloaded/reloaded during a scene change, so simply *displaying* a previously loaded scene for a second/third/etc time will NOT cause that code to reload/re-execute.

you can fix this by recycling old scenes to unload their code, forcing a reload on next display.  (as others have already mentioned)

or you can fix it without recycling by getting into the habit of not *assigning* values at module scope.  it’s ok to *declare* those free-floating locals at the top of your module, but assign their values in either create (if it needs to happen just once) or in show (if it needs to happen each time scene displayed).  then clean up correspondingly in either destroy or hide.