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