Ver Fonte

Update fade_in_out.script

Björn Ritzl há 5 anos atrás
pai
commit
c516f12813
1 ficheiros alterados com 13 adições e 40 exclusões
  1. 13 40
      examples/sound/fade_in_out/fade_in_out.script

+ 13 - 40
examples/sound/fade_in_out/fade_in_out.script

@@ -1,49 +1,22 @@
-go.property("gain", 1) -- <1>
-
-local TIME = 2 -- <2>
-local DELAY = 1 -- <3>
+local TIME = 2 -- <1>
+local DELAY = 1 -- <2>
 
 function init(self)
-    msg.post("#music", "play_sound") -- <4>
-end
-
-local function fade_in(self) -- <5>
-    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)
+    sound.play("#music", { gain = 1.0 }) -- <3>
+    msg.post("#", "fade_in_out") -- <4>
 end
 
-local function fade_out(self) -- <6>
-    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 -- <7>
-        msg.post("#music", "set_gain", {gain = self.gain}) -- <8>
-    else
-        if self.gain == 0 then
-            fade_in(self) -- <9>
-        elseif self.gain == 1 then
-            fade_out(self) -- <10>
-        end
+function on_message(self, message_id, message, sender)
+    if message_id == hash("fade_in_out") then
+        go.animate("#music", "gain", go.PLAYBACK_LOOP_PINGPONG, 0, go.EASING_LINEAR, TIME, DELAY) -- <5>
     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
+1. Create TIME constant - duration of the fade-in and fade-out effect.
+2. Create DELAY constant - pause before the start of the fade-in and fade-out effect.
+3. Tell the component "#music" to start playing its sound with a gain of 1.0
+4. Send a "fade_in_out" message to the script telling it to start fading the music in and out
+5. Animate the "gain" property of the sound component back and forth between 0 and the current value (1.0)
 --]]