I was wondering if we could have a built in tween function for gain on sounds. I’d love to be able to crossfade my looping music dynamically and in a smooth manner. It seems like it would be useful. I’m sure I could do it by hand with a bunch of nested function but I’d rather not have to.
What I’m doing currently:
go.property("crossfadeVariable", 0)
local startGain = 0.0
local stopGain = 1.0
function crossfade_audio(start, stop)
if startGain < 1.0 and stopGain > 0.0 then
go.animate("#", "crossfadeVariable", go.PLAYBACK_ONCE_FORWARD, 1, go.EASING_LINEAR, 0.025, 0, function ()
startGain = startGain + 0.1
stopGain = stopGain - 0.1
msg.post(start, "set_gain", {gain = startGain})
msg.post(stop, "set_gain", {gain = stopGain})
crossfade_audio(start, stop)
end)
elseif
msg.post(stop, "stop_sound")
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("to_main_menu") then
if self.state ~= "MAIN_MENU" then
msg.post("#boardproxy", "unload")
startGain = 0.0
stopGain = 1.0
msg.post("#MenuMusic", "play_sound", { gain = startGain })
crossfade_audio("#BoardMusic","#MenuMusic")
else
msg.post("#MenuMusic", "play_sound", { gain = 1.0 })
end
msg.post("main:/main#menu", "enable")
msg.post("#background", "enable")
self.state = "MAIN_MENU"
elseif message_id == hash("start_game") then
msg.post("#background", "disable")
msg.post("#boardproxy", "load")
msg.post("#menu", "disable")
elseif message_id == hash("proxy_loaded") then
-- Board collection has loaded...
msg.post(sender, "init")
startGain = 0.0
stopGain = 1.0
msg.post("#BoardMusic", "play_sound", { gain = startGain })
crossfade_audio("#BoardMusic","#MenuMusic")
msg.post("board:/board#script", "start_level")
msg.post(sender, "enable")
self.state = "GAME_RUNNING"
end
end