我正在处理我的菜单结构和选项菜单。我正在使用Director API在场景之间转换并使用自定义保存/加载系统。
我正在努力让用户可以打开和关闭音乐。我已经让它工作了,但不是以最优化的方式(因为它会停止所有声音)。
Main.lua
bgMusic = audio.loadSound( "sounds/Torukia.mp3" )
musicIsPlaying = false
mainMenu.lua
if musicIsPlaying == false then
if gameSettings.soundOn == true then
backgroundMusicChannel = audio.play( bgMusic, { loops=-1 } )
musicIsPlaying = true
elseif gameSettings.soundOff == true then
musicIsPlaying = false
end
end
end
optionsMenu.lua
local function toggleSound( event )
if event.phase == "ended" then
if gameSettings.soundOn == true then
gameSettings.soundOn = false
gameSettings.soundOff = true
audio.stop( )
soundBtn:setFillColor( 255,0,0 )
elseif gameSettings.soundOff == true then
gameSettings.soundOn = true
gameSettings.soundOff = false
backgroundMusicChannel = audio.play( bgMusic, { loops=-1 } )
soundBtn:setFillColor( 0,255,0 )
end
saveSettings(gameSettings, "gameSettings.json")
end
end
我可以制作全球声道吗?这会有所帮助。我已经尝试查看文档并搜索答案,但我对此问题找不到多少。
答案 0 :(得分:1)
试试此版本,您可以指定要播放或停止的频道
Main.lua
bgMusicChannel = 1 -- can be 1-32
bgMusic = audio.loadSound( "sounds/Torukia.mp3" )
musicIsPlaying = false
mainMenu.lua
if musicIsPlaying == false then
if gameSettings.soundOn == true then
backgroundMusicChannel = audio.play( bgMusic, { loops=-1,channel = bgMusicChannel } )
musicIsPlaying = true
elseif gameSettings.soundOff == true then
musicIsPlaying = false
end
end
end
optionsMenu.lua
local function toggleSound( event )
if event.phase == "ended" then
if gameSettings.soundOn == true then
gameSettings.soundOn = false
gameSettings.soundOff = true
audio.stop( bgMusicChannel )
soundBtn:setFillColor( 255,0,0 )
elseif gameSettings.soundOff == true then
gameSettings.soundOn = true
gameSettings.soundOff = false
backgroundMusicChannel = audio.play( bgMusic, { loops=-1, channel = bgMusicChannel } )
soundBtn:setFillColor( 0,255,0 )
end
saveSettings(gameSettings, "gameSettings.json")
end
end