gpt4 book ai didi

button - 声音和音乐Corona SDK的2个开关按钮?

转载 作者:行者123 更新时间:2023-12-03 00:35:55 25 4
gpt4 key购买 nike

我需要有关小部件切换按钮的帮助。我创建了2个切换按钮,用于声音切换和音乐切换,但是问题是每次我关闭并打开音乐开关时,音乐(mp3声音)都没有响应,这意味着每次我打开音乐时其速度都会快进/关闭。下一个问题是每当我关闭声音开关时,它也会关闭音乐(mp3声音)。
这是我的代码:

--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

最佳答案

我不确定您的方式是否正确。我是初学者,但我想帮助您:)

从Corona文档中了解 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

阅读有关 audio的更多信息。

关于button - 声音和音乐Corona SDK的2个开关按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41426085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com