Im using a tutorial from Jay on mastering corona sdk about using widgets on an option page to toggle sound on and off. Im using GGData and have followed all the steps in the video but am having a issue where the music isn’t toggling. Sound effects are toggling on and off perfectly well but I am guessing that it is because sfx doesn’t play until the game starts playing, where as music plays on menu screen.
I have also noticed that when I toggle music checkbox to off, and restart the simulator, it works fine and doesn’t load any music unless i turn it on. Whats going on? I really am confused.
Ill do my best to post relevant code up now however, if I miss something then please tell me and i will post it up.
Ps: storyboard has been required in all scenes, I just haven’t included it in the code below.
Main.lua
local GGData = require("GGData") local prefs = GGData:new("preferences") musicIsPlaying = true sfxIsPlaying = true local function loadPrefs() prefs:load() musicIsPlaying = prefs.musicIsPlaying sfxIsPlaying = prefs.sfxIsPlaying end function savePrefs() prefs.musicIsPlaying = musicIsPlaying prefs.sfxIsPlaying = sfxIsPlaying prefs:save() end audio.reserveChannels(1) sndChanMusic = 1 sndJump = audio.loadSound("sounds/sfx.wav") sndMusic = audio.loadStream("sounds/POL-air-sharks-short.wav") function playSFX(audioHandle, opt) local options = opt or {} local loopNum = options.loop or 0 local channel = options.channel or 0 local chanUsed = nil if sfxIsPlaying then chanUsed = audio.play( audioHandle, { channel=channel, loops=loopNum } ) end return chanUsed end function playMusic() if musicIsPlaying then audio.play( sndMusic, {channel = sndChanMusic, loops=-1 } ) audio.setVolume ( .15 ,{ channel=sndChanMusic } ) end end loadPrefs()
Menu.lua
function scene:enterScene( event ) playMusic()
Setting.lua
local musicSwitch = widget.newSwitch { top = display.contentCenterY - 70, left = display.contentCenterX + 40, style = "checkbox", initialSwitchState = musicIsPlaying, onRelease = function() musicIsPlaying = not musicIsPlaying end } group:insert(musicSwitch) local sfxSwitch = widget.newSwitch { top = display.contentCenterY - 15, left = display.contentCenterX +40, style = "checkbox", initialSwitchState = sfxIsPlaying, onRelease = function() sfxIsPlaying = not sfxIsPlaying end } group:insert(sfxSwitch) function goBack(event) if event.phase == "began" then savePrefs() storyboard.gotoScene("menu", "fade", 500) end end