2017-01-02 118 views
0

我需要關於我的小部件切換按鈕的幫助。我已經創建了2個用於聲音切換和音樂切換的切換按鈕,但問題在於,每當我關閉並打開音樂切換開關時,音樂(mp3音效)都無法響應,這意味着每當我切換音樂時,其速度都會快速前進開/關。接下來的問題是每次我關掉聲音開關,它也關掉音樂(MP3聲音)​​。 繼承人我的代碼:2個用於聲音和音樂Corona SDK的開關按鈕?

--utils.lua

local sounds = {} 
sounds["select"] = audio.loadSound("sounds/select.mp3") 
sounds["score"] = audio.loadSound("sounds/score.mp3") 
sounds["incorrect"] = audio.loadSound("sounds/gameover.mp3") 
sounds["clap"] = audio.loadSound("sounds/clapping.mp3") 
sounds["music"] = audio.loadSound("sounds/gameMusic.mp3") 

M.playSound = function(name) 
    if sounds[name] ~= nil then 
     audio.play(sounds[name]) 
    end 
end 

--Settings.lua

soundSwitchPressed = function(event) 
local switch = event.target 
utils.playSound("select") 


if switch.id == "sound" then 
    if switch.isOn == true then 
     audio.setVolume(0) 
    else 
     audio.setVolume(1) 
    end 
end 
end 

musicSwitchPressed = function(event) 
    local switch = event.target 
    utils.playSound("music") 

    if switch.id == "music" then 
     if switch.isOn == true then 
      audio.setVolume(0) 
     else 
      audio.setVolume(1) 
     end 

    end 
end 



local sound_switch = widget.newSwitch 
    { 
     left = _W-70, 
     top = navBar.y + navBar.height/2 + 44, 
     style = "onOff", 
     id = "sound", 
     x = 800, 
     y = 960, 
     onPress = soundSwitchPressed 
    } 
    sound_switch.xScale, sound_switch.yScale = 3, 3 
    uiGroup:insert(sound_switch) 

    local music_switch = widget.newSwitch 
    { 
     left = _W-70, 
     top = navBar.y + navBar.height/2 + 44, 
     style = "onOff", 
     id = "music", 
     x = 800, 
     y = 1200, 
     onPress = musicSwitchPressed 
    } 

    if audio.getVolume() == 0 then 
     sound_switch:setState({isOn=false, isAnimated=false}) 
     music_switch:setState({isOn=false, isAnimated=false}) 
    else 
     sound_switch:setState({isOn=true, isAnimated=false}) 
     music_switch:setState({isOn=true, isAnimated=false}) 
    end 
end 

enter image description here

回答

1

我不知道你的方式是好的。我beginer但我想幫你:)

從電暈文檔中關於audio.setVolume()

Sets the volume either for a specific channel, or sets the master volume.

所以audio.setVolume()影響所有的聲音和音樂。

也許嘗試使用變量來確定是否播放聲音和音樂。

utils.lua

audio.reserveChannels(6) 
... 
sounds["select"] = audio.loadSound("sounds/select.mp3") 
sounds["score"] = audio.loadSound("sounds/score.mp3") 
sounds["incorrect"] = audio.loadSound("sounds/gameover.mp3") 
sounds["clap"] = audio.loadSound("sounds/clapping.mp3") 
sounds["music"] = audio.loadSound("sounds/gameMusic.mp3") 

local channels = {} 
sounds["select"] = 1 
sounds["score"] = 2 
sounds["incorrect"] = 3 
sounds["clap"] = 4 
sounds["music"] = 5 

music = audio.loadStream("backgroundMusic.mp3") 

M.soundOn = true 
M.musicOn = true 

M.playMusic = function() 
    if music ~= nil then 
     audio.play(music, { channel = 6 }) 
    end 
end 

M.playSound = function(name) 
     if sounds[name] ~= nil then 
      audio.play(sounds[name], { channel = channels[name]}) 
     end 
    end 

Settings.lua

... 
soundSwitchPressed = function(event) 
local switch = event.target 

if utils.soundOn then 
    utils.playSound("select") 
end 

if switch.id == "sound" then 
    if switch.isOn == true then 
     utils.soundOn = true 
    else 
     utils.soundOn = false 
     audio.stop(1) 
     audio.stop(2) 
     audio.stop(3) 
     audio.stop(4) 
     audio.stop(5) 
    end 
end 
end 
... 

musicSwitchPressed = function(event) 
    local switch = event.target 

    if utils.musicOn then 
     utils.playSound("music") 
    end 

    if switch.id == "music" then 
     if switch.isOn == true then 
      utils.musicOn = true 
      utils.playMusic() 
     else 
      utils.musicOn = false 
      audio.stop(6) 
     end 

    end 
end 

每當你的聲音將代碼

if utils.soundOn then 
    utils.playSound("your_sound_effect_name") 
end 

if utils.musicOn then 
    utils.playMusic() 
end 

查看更多about audio

+0

Idurniat先生是否有另一種聲明頻道的方法?恩。聲音[「選擇」]。通道= 1 < - im與這種聲明錯誤,它說錯誤:嘗試索引字段'選擇'(用戶數據值)..順便說一句我試過這聽起來[「拍手」 ] = audio.loadSound(「sounds/clapping.mp3」,{channel = 1})仍然有相同的錯誤...生病了解更多關於audios ... –

+0

我已經改善了我的答案。它現在應該工作。這是不正確的語法'sounds [「clap」] = audio.loadSound(「sounds/clapping.mp3」,{channel = 1})'當您播放,停止等時需要使用'channel'參數,例如'audio .stop(1)'或'audio.setVolume(0,{channel = 1})'。 – ldurniat

+0

你真的是一個很好的幫助idurniat,但我怎麼能夠保存我的開關按鈕的動畫?點擊後退按鈕後,我的開關不會自我保存。我編輯代碼後,我的if-else語句audio.getVolume沒有保存我的動畫切換按鈕。但我試過如果audio.stop()==零。仍然不起作用。 –