Turning music on and off

Hi all,

I have created a settings page which contains a switch widget (On and Off switch) and I have created a function like so:

local function onSwitchPress( event )         local switch = event.target         local response = switch.id.." is on: "..tostring( switch.isOn )         print( response )     if (switch.isOn == true) then         print("It's on")         gameFile.kickSound = audio.loadSound("sounds/kick.mp3")         gameFile.defencePop = audio.loadSound("sounds/pop.mp3")         gameFile.winSound = audio.loadSound("sounds/win.mp3")         gameFile.loseSound = audio.loadSound("sounds/lose.mp3")     elseif (switch.isOn == false) then         print("It's off")                  gameFile.kickSound = nil         gameFile.defencePop = nil         gameFile.winSound = nil         gameFile.loseSound = nil     end     end  

kickSound, defencePop, winSound and loseSound are in another file called ‘game’ and are global (and set as nil). I have also called the ‘require’ method at the start of this file.

When I run the game, there is no music. When I restart the program, go to the settings page, even if the switch is On or Off, when I return to the game, music still plays. 

How do I solve this?

It’s not exactly clear from your description exactly what issue you’re having.  Could you clarify what behavior you want to happen, and what behavior is happening instead?  The more precise, the better.

Are you trying to set it up so that your settings save even after the app is restarted?  In other words, if the user turned the music off, then next time they open the app, the music will still be off, and likewise if they turned it on, then next time they open the app the music will be on?  If so, then you’ll have to save the settings to a file and load them each time your app starts.

  • Andrew

It’s not exactly clear from your description exactly what issue you’re having.  Could you clarify what behavior you want to happen, and what behavior is happening instead?  The more precise, the better.

Are you trying to set it up so that your settings save even after the app is restarted?  In other words, if the user turned the music off, then next time they open the app, the music will still be off, and likewise if they turned it on, then next time they open the app the music will be on?  If so, then you’ll have to save the settings to a file and load them each time your app starts.

  • Andrew