storyboard - how/where to add/remove listeners for a button widget in storyboard?

Getting confused re what I should be doing re use of widget.newButton in storyboard, in terms of how to ensure the listener is removed in the “exitScene” and resetup again in the “enterScene”?

Conona/someone - Are you able to confirm with a short piece of sample code how to carry this out?

Related items/sub-questions:

a) do you have to not use the “onRelease” type field when you setup the widget, but then manually add it yourself in the “enterScene”

b) same question re whether you have to manually remove the listener yourself in exitScene, and then in destroyScene to the myWidgetButton.remove?

c) if you don’t/can’t add/remove the widget button listener yourself manually, then what happens re scene caching? and does this impact your overall use of scene caching for that scene then?

i Have the same question, I want to work in storyboard to make a book app. maybe you know more than me and can share what you know.

What is the fisrt thing to know about storyboards, and how you make it work?

best to post your question separate and in the storyboard section I think

We covered this in a blog post a couple of weeks ago:

http://www.coronalabs.com/blog/2013/04/02/cleaning-up-display-objects-andlisteners/

Basically if it’s a display object, widget and it’s in the storyboard scene’s “view” i.e. “group”, then you don’t have to worry about cleaning it up.  You only need to worry about cleaning up Runtime listeners.

Hi Rob,

I read this article http://www.coronalabs.com/blog/2013/04/02/cleaning-up-display-objects-andlisteners/


A common question in the forums and elsewhere is how to properly and thoroughly clean up scenes when using a scene manager like Storyboard or Director.

You need to understand how modules behave in Lua and how that impacts Storyboard. When you do…

storyboard.gotoScene( "myscene" )

…somewhere in the process the following code executes:

local myscene = require( "myscene" )

In Lua, if you load a module multiple times, only one copy is loaded,
and it stays in memory until it’s specifically un-required. This is why
we can have persistent values that survive between scenes. Storyboard
scenes are modules that have a main chunk and several functions that are
called by dispatched events. If your scene module looks like this:


But I think it;s too advanced for me, how do I star understanding the goToScene stuff from the very beginning?

In a simple words, not in a CODE PROGRAMMING words…

Please

Thank you

Victor

Oh another question…

How can I post a video from YOUTUBE here?

I saw someone else in another part of this forum, and he had a video posted.

thanks

Victor

thanks Rob - actually another question that would be useful to ask (following on from Victor) is:

Q - Is there a way to log what listeners (if any) have not be validly been removed after the “scene:exitScene”?   That is a way to test from a storyboard best practice approach that you can get Corona to prove to you you are following the process?  Perhaps there is a way to iterate through a scene’s attached listeners at the beginning of a “scene.enterScene” method, such that there should really be no listeners present at this point? 

@greg886 Since you only need to worry about Runtime listeners that you add, it should be easy enough to remove them.  There is no list to get them.  Non-runtime listeners should go away with the objects they are attached to…

@Victor, see:

http://www.coronalabs.com/blog/2011/11/14/introducing-the-storyboard-api/

http://www.coronalabs.com/blog/2012/08/21/storyboard-basic-usage/

http://www.coronalabs.com/blog/2011/11/16/common-storyboard-api-questions/

http://www.coronalabs.com/blog/2012/03/27/storyboard-scene-events-explained/

http://www.coronalabs.com/blog/2012/07/31/storyboard-scene-purging-vs-removal/

Just to clarify Rob, I’m assume when you say they “should go away” you mean when a scene is “destroyed”?   What I was getting about was when you try to adhere to the Storyboard caching approach where you add/remove listeners with each scene “enterScene” and “exitScene”…  

So I get my question would be:

a] is it true to say that any auto-removal of non-Runtime listeners (e.g. listeners on display objects) would NOT occur after an “exitScene”, but rather at the “destroyScene” stage?

b] in other words it would be up to us to manage manually the creation & removal of listeners in a Storyboard “enterScene” and “exitScene” event if we want to follow the storyboard caching approach?

thanks

If you create a display object:

[lua]

    button = display.newImageRect(…)

    local function fred(event)

        …

    end

    button:addEventListener(“touch”, fred)

    group:insert(button)

[/lua]

in either createScene or enterScene and you add it to the scene’s view, i.e. group:insert… then when the scene is transitioned away, that button is no longer on screen so it can’t be interacted with.  When the scene is purged or removed, those objects are destroyed.  The event handler itself is just a function in code, it it doesn’t really get destroyed, but the objects isn’t there so there is no way to trigger the event.  So yes, your a) assumption is right sort of.  You don’t have to deal with it, but when the process that calls destroyScene() is running, your objects go away.

I’m not completely sure I follow what you mean by “Caching approach”, but if you mean that we keep the scene in memory in case you come back to it, when those objects transition back on screen, you can interact with them again and the existing event listeners you added when the object was first created are still there and functioning. 

thanks Rob (for bearing with me)

So it’s true to say then whilst the original storyboard tutorials (e.g. http://www.coronalabs.com/blog/2011/11/14/introducing-the-storyboard-api/ ) highlighted removing listeners in the “exitScene” event, in fact you don’t really need to do this for non-Runtime listeners (e.g. display objects).   So in all my scene’s I could pretty much remove these “removeEventListener” lines for my display objects and save some code correct?

Also there would be no issues then either adding the display object listeners in createScene if you wanted, instead of enterScene  then too correct?  (otherwise you’d be added them back in each enterScene when you wouldn’t have needed to, because they wouldn’t have been removed)

i Have the same question, I want to work in storyboard to make a book app. maybe you know more than me and can share what you know.

What is the fisrt thing to know about storyboards, and how you make it work?

best to post your question separate and in the storyboard section I think

We covered this in a blog post a couple of weeks ago:

http://www.coronalabs.com/blog/2013/04/02/cleaning-up-display-objects-andlisteners/

Basically if it’s a display object, widget and it’s in the storyboard scene’s “view” i.e. “group”, then you don’t have to worry about cleaning it up.  You only need to worry about cleaning up Runtime listeners.

Thank you everyone for helping me, THANK YOU VERY MUCH!

I’m learning little by little, but I’m almost there to start making my books.

One more question – I’m adding a video to try to explaing the question better

http://www.youtube.com/watch?v=waa_Troorf4&feature=youtu.be

Thank you again for all your help.

I got the sound –

the storyboard –

the animation –

the background –

Good!

Victor

Hi Rob,

I read this article http://www.coronalabs.com/blog/2013/04/02/cleaning-up-display-objects-andlisteners/


A common question in the forums and elsewhere is how to properly and thoroughly clean up scenes when using a scene manager like Storyboard or Director.

You need to understand how modules behave in Lua and how that impacts Storyboard. When you do…

storyboard.gotoScene( "myscene" )

…somewhere in the process the following code executes:

local myscene = require( "myscene" )

In Lua, if you load a module multiple times, only one copy is loaded,
and it stays in memory until it’s specifically un-required. This is why
we can have persistent values that survive between scenes. Storyboard
scenes are modules that have a main chunk and several functions that are
called by dispatched events. If your scene module looks like this:


But I think it;s too advanced for me, how do I star understanding the goToScene stuff from the very beginning?

In a simple words, not in a CODE PROGRAMMING words…

Please

Thank you

Victor

Oh another question…

How can I post a video from YOUTUBE here?

I saw someone else in another part of this forum, and he had a video posted.

thanks

Victor

thanks Rob - actually another question that would be useful to ask (following on from Victor) is:

Q - Is there a way to log what listeners (if any) have not be validly been removed after the “scene:exitScene”?   That is a way to test from a storyboard best practice approach that you can get Corona to prove to you you are following the process?  Perhaps there is a way to iterate through a scene’s attached listeners at the beginning of a “scene.enterScene” method, such that there should really be no listeners present at this point? 

@greg886 Since you only need to worry about Runtime listeners that you add, it should be easy enough to remove them.  There is no list to get them.  Non-runtime listeners should go away with the objects they are attached to…

@Victor, see:

http://www.coronalabs.com/blog/2011/11/14/introducing-the-storyboard-api/

http://www.coronalabs.com/blog/2012/08/21/storyboard-basic-usage/

http://www.coronalabs.com/blog/2011/11/16/common-storyboard-api-questions/

http://www.coronalabs.com/blog/2012/03/27/storyboard-scene-events-explained/

http://www.coronalabs.com/blog/2012/07/31/storyboard-scene-purging-vs-removal/

Just to clarify Rob, I’m assume when you say they “should go away” you mean when a scene is “destroyed”?   What I was getting about was when you try to adhere to the Storyboard caching approach where you add/remove listeners with each scene “enterScene” and “exitScene”…  

So I get my question would be:

a] is it true to say that any auto-removal of non-Runtime listeners (e.g. listeners on display objects) would NOT occur after an “exitScene”, but rather at the “destroyScene” stage?

b] in other words it would be up to us to manage manually the creation & removal of listeners in a Storyboard “enterScene” and “exitScene” event if we want to follow the storyboard caching approach?

thanks