Cleaning up and Storyboard

Hi,

Suppose that we have:

function scene:createScene( event )     local group = self.view     local frame1Intro = display.newImage( "images/Frame1Intro.png", \_G.centerX, \_G.centerY )     frame1Intro.alpha = 0     group:insert( frame1Intro )     local introMusic = audio.loadStream( "sounds/intro.ogg" )     audio.reserveChannels( 1 )     audio.setVolume( 1.0, { channel = 1 } )     audio.play( introMusic, { channel = 1 } )     transition.to( frame1Intro, { time = 8000, alpha = 1, onComplete = onCompleteFrame1 } ) end  

My questions is: Should I remove (removeSelf, nil out, dispose, etc) those objects? Or they

that be done by Corona after scene:destroyScene?

Thanks!
Angelo

i  think since you insert these objects in a gruop  they will clean them self up atomatically ,  when i am writing my code i like to put

storyboard.removeAll()

 at  the top of my code, i think it  make storybard cleaner for me.

hope this help

Sean:

From Boxing Studio Games

Any display.* object you create and install in to “group” will be disposed for you.

However that audio you are loading will need to be disposed of in the destroyScene() event.

Rob

Thanks Sean and Rob.

That’s the point! In that example how can I dispose the audio in the destroyScene( ) if I don’t have reference to the audio handle?

If when loading the sound I do something like self.introMusic = …, then, OK, I’ll have access to it in the destroyScene( ), but since that’s a local variable (to the function) what should I do? What’s the right/usual approach?

In the destroy scene call audio.dispose(intoMusic)
That show do the trick of cleaning up your audio

Any questions ask me

Sean:
From Boxing Studio Games

When you do this inside of createScene():

local introMusic = audio.loadStream( “sounds/intro.ogg” )

You are making introMusic only visible to the createScene function.  This is a term in programming known as “Scope”.  There are two solutions that you can do.

First, declare introMusic at the top of your module outside of the createScene function, but do not load the audio there:

local introMusic 

Then inside createScene, leave off the local:

introMusic = audio.loadStream( "sounds/intro.ogg" )

At that point, destroyScene can access introMusic just fine.  However this is know as an “Up Value” and you can only have 60 of them in a given Lua block of code.  Perhaps the better way, which is more object oriented is to assign the value to the scene object, something like:

self.introMusic = audio.loadStream( "sounds/intro.ogg" ) audio.play( self.introMusic, { channel = 1 } )

Then in destroyScene() you could then do:

audio.destory( self.introMusic )

Now there is some danger with this method. While it’s cleaner and better because of the upvalue issue, it has the draw back of you not knowing what all things we have done in the scene object already.  I mean “introMusic” is probably a safe variable name but what happens if we release a new version and we decide to start using the name “introMusic” as a variable/method?  Then you would be potentially writing over one of our variables.  What you should do in this case is a technique known as “Name Spacing”, where you prepend a string at the beginning of your variables that we are very unlikely to use, say your initials, in my case:

self.rwm\_introMusic = audio.loadStream( "sounds/intro.ogg" ) audio.play( self.rwm\_introMusic, { channel = 1 } )

That pretty much protects your variable names and our variable names from clashing.  The other technique is to create a data table inside of the scene object that is yours and always add your variables to your table.   When we developed the new version of Storyboard that we call Composer (available to Pro and Enterprise subscribers through daily builds), we added a new feature called .setVariable and .getVariable where you can add things to the scene in a safe way without having to name space your variable names.

Rob

Thank you very much Sean and Rob :slight_smile:

Any time :slight_smile:

Hi,

Could please take a look at this code? I’m getting: attempt to call method ‘removeSelf’ (a nil value). What’s wrong?

The ideia is to cancel/remove timers when going to other scenes…

function scene:enterScene( event ) local function message( ) print( "timer1" ) end self.timer1 = timer.performWithDelay( 5000, message ) storyboard.gotoScene( "otherScene", { time = 500, effect = "crossFade" } ) end function scene:exitScene( event ) self.timer1:cancel( ) self.timer1:removeSelf( ) end

I think you should just do Timer.cancle(your timer id ) and then I believe that you could set it to nil

Sean:

From Boxing Studio Games

I would just do Timer.cancle (your id)
For example Timer.cancle ( timer1)
Your error mean your disposing something that doesn’t exist
Sean:
from Boxing Studio Games

Timers do not support a removeSelf() method.  After you cancel it, you can simply nil it.

Rob

Of course! Thank you again Rob and Sean :slight_smile:

anytime 

Sean:

From Boxing Studio Games  

i  think since you insert these objects in a gruop  they will clean them self up atomatically ,  when i am writing my code i like to put

storyboard.removeAll()

 at  the top of my code, i think it  make storybard cleaner for me.

hope this help

Sean:

From Boxing Studio Games

Any display.* object you create and install in to “group” will be disposed for you.

However that audio you are loading will need to be disposed of in the destroyScene() event.

Rob

Thanks Sean and Rob.

That’s the point! In that example how can I dispose the audio in the destroyScene( ) if I don’t have reference to the audio handle?

If when loading the sound I do something like self.introMusic = …, then, OK, I’ll have access to it in the destroyScene( ), but since that’s a local variable (to the function) what should I do? What’s the right/usual approach?

In the destroy scene call audio.dispose(intoMusic)
That show do the trick of cleaning up your audio

Any questions ask me

Sean:
From Boxing Studio Games

When you do this inside of createScene():

local introMusic = audio.loadStream( “sounds/intro.ogg” )

You are making introMusic only visible to the createScene function.  This is a term in programming known as “Scope”.  There are two solutions that you can do.

First, declare introMusic at the top of your module outside of the createScene function, but do not load the audio there:

local introMusic 

Then inside createScene, leave off the local:

introMusic = audio.loadStream( "sounds/intro.ogg" )

At that point, destroyScene can access introMusic just fine.  However this is know as an “Up Value” and you can only have 60 of them in a given Lua block of code.  Perhaps the better way, which is more object oriented is to assign the value to the scene object, something like:

self.introMusic = audio.loadStream( "sounds/intro.ogg" ) audio.play( self.introMusic, { channel = 1 } )

Then in destroyScene() you could then do:

audio.destory( self.introMusic )

Now there is some danger with this method. While it’s cleaner and better because of the upvalue issue, it has the draw back of you not knowing what all things we have done in the scene object already.  I mean “introMusic” is probably a safe variable name but what happens if we release a new version and we decide to start using the name “introMusic” as a variable/method?  Then you would be potentially writing over one of our variables.  What you should do in this case is a technique known as “Name Spacing”, where you prepend a string at the beginning of your variables that we are very unlikely to use, say your initials, in my case:

self.rwm\_introMusic = audio.loadStream( "sounds/intro.ogg" ) audio.play( self.rwm\_introMusic, { channel = 1 } )

That pretty much protects your variable names and our variable names from clashing.  The other technique is to create a data table inside of the scene object that is yours and always add your variables to your table.   When we developed the new version of Storyboard that we call Composer (available to Pro and Enterprise subscribers through daily builds), we added a new feature called .setVariable and .getVariable where you can add things to the scene in a safe way without having to name space your variable names.

Rob

Thank you very much Sean and Rob :slight_smile: