0

我目前正在研究我的菜单结构和选项菜单。我正在使用 Director API 在场景之间进行转换并使用自定义保存/加载系统。

我正在努力做到这一点,以便用户可以打开和关闭音乐。我已经让它工作了,但不是以最优化的方式(因为它停止了所有声音)。

主程序.lua

bgMusic = audio.loadSound( "sounds/Torukia.mp3" )
musicIsPlaying = false

主菜单.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

我可以创建全局声道吗?那会很有帮助。我试过查看文档并寻找答案,但我找不到关于这个问题的太多信息。

4

1 回答 1

1

试试这个版本,你可以指定你想播放或停止哪个频道

主程序.lua

bgMusicChannel = 1 -- can be 1-32
bgMusic = audio.loadSound( "sounds/Torukia.mp3" )
musicIsPlaying = false

主菜单.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
于 2013-03-01T02:47:50.867 回答