- Select manual -

Fade In-Out

This example shows how to make Fade-In and fade Fade-Out music.

fade_in_out

fade_in_out.script

go.property("gain", 1) 

local TIME = 2 
local DELAY = 1 

function init(self)
    msg.post("#music", "play_sound") 
end

local function fade_in(self) 
    self.in_fade_now = true
    go.animate("#", "gain", go.PLAYBACK_ONCE_FORWARD, 1, go.EASING_LINEAR, TIME, DELAY, 
    function() 
        self.in_fade_now = false 
    end)
end

local function fade_out(self) 
    self.in_fade_now = true
    go.animate("#", "gain", go.PLAYBACK_ONCE_FORWARD, 0, go.EASING_LINEAR, TIME, DELAY,
    function() 
        self.in_fade_now = false 
    end)
end

function update(self, dt)
    if self.in_fade_now then 
        msg.post("#music", "set_gain", {gain = self.gain}) 
    else
        if self.gain == 0 then
            fade_in(self) 
        elseif self.gain == 1 then
            fade_out(self) 
        end
    end
end
  1. Create “gain” property that can be animated using go.animate().
  2. Create TIME constant - duration of the fade-in and fade-out effect.
  3. Create DELAY constant - pause before the start of the fade-in and fade-out effect.
  4. Send message to component “#music” telling it to start playing its sound.
  5. Create fade_in() method where we animate our “gain” property to 1.
  6. Create fade_out() method where we animate our “gain” property to 0.
  7. Check flag self.in_fade_now and send message only if this flag is true.
  8. Send message “set_gain” with the “gain” property value to the sound component.
  9. Start fade_in if gain property is equal to 0
  10. Start fade_out if gain property is equal to 1

If you want to play with these examples, you can get the project on Github.

Do you want to see more examples? Why not write a few yourself and submit a pull request? We love contributions.

Contribute