Explorar el Código

Merge branch 'master' of https://github.com/defold/examples

Björn Ritzl hace 7 años
padre
commit
fc140dbbbb

+ 18 - 0
examples/_main/loader.go

@@ -478,3 +478,21 @@ embedded_components {
     w: 1.0
   }
 }
+embedded_components {
+  id: "sound/fade_in_out"
+  type: "collectionproxy"
+  data: "collection: \"/examples/sound/fade_in_out/fade_in_out.collection\"\n"
+  "exclude: false\n"
+  ""
+  position {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+  }
+  rotation {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+    w: 1.0
+  }
+}

+ 1 - 1
examples/_main/menu.gui_script

@@ -106,7 +106,7 @@ function init(self)
 	self.index["gui"] = { "button", "stencil", "load_texture", "pointer_over"}
 	self.index["input"] = { "move", "text", "down duration" }
 	self.index["particles"] = { "particlefx", "modifiers" }
-	self.index["sound"] = { "music" }
+	self.index["sound"] = { "music", "fade_in_out" }
 	self.index["render"] = { "lorem ipsum" }
 	self.index["camera"] = { "lorem ipsum" }
 	self.index["debug"] = { "physics", "profile" }

+ 131 - 0
examples/sound/fade_in_out/fade_in_out.collection

@@ -0,0 +1,131 @@
+name: "default"
+scale_along_z: 0
+embedded_instances {
+  id: "gameobject"
+  data: "components {\n"
+  "  id: \"script\"\n"
+  "  component: \"/examples/sound/fade_in_out/fade_in_out.script\"\n"
+  "  position {\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
+  "    z: 0.0\n"
+  "  }\n"
+  "  rotation {\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
+  "    z: 0.0\n"
+  "    w: 1.0\n"
+  "  }\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"sprite\"\n"
+  "  type: \"sprite\"\n"
+  "  data: \"tile_set: \\\"/assets/sprites.atlas\\\"\\n"
+  "default_animation: \\\"bunny2_ready\\\"\\n"
+  "material: \\\"/builtins/materials/sprite.material\\\"\\n"
+  "blend_mode: BLEND_MODE_ALPHA\\n"
+  "\"\n"
+  "  position {\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
+  "    z: 0.0\n"
+  "  }\n"
+  "  rotation {\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
+  "    z: 0.0\n"
+  "    w: 1.0\n"
+  "  }\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"label\"\n"
+  "  type: \"label\"\n"
+  "  data: \"size {\\n"
+  "  x: 128.0\\n"
+  "  y: 32.0\\n"
+  "  z: 0.0\\n"
+  "  w: 0.0\\n"
+  "}\\n"
+  "scale {\\n"
+  "  x: 0.5\\n"
+  "  y: 0.5\\n"
+  "  z: 1.0\\n"
+  "  w: 0.0\\n"
+  "}\\n"
+  "color {\\n"
+  "  x: 0.0\\n"
+  "  y: 0.5647059\\n"
+  "  z: 0.99215686\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "outline {\\n"
+  "  x: 1.0\\n"
+  "  y: 1.0\\n"
+  "  z: 1.0\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "shadow {\\n"
+  "  x: 1.0\\n"
+  "  y: 1.0\\n"
+  "  z: 1.0\\n"
+  "  w: 1.0\\n"
+  "}\\n"
+  "leading: 1.0\\n"
+  "tracking: 0.0\\n"
+  "pivot: PIVOT_CENTER\\n"
+  "blend_mode: BLEND_MODE_ALPHA\\n"
+  "line_break: false\\n"
+  "text: \\\"Dig the tune...\\\"\\n"
+  "font: \\\"/assets/text48.font\\\"\\n"
+  "material: \\\"/builtins/fonts/label.material\\\"\\n"
+  "\"\n"
+  "  position {\n"
+  "    x: 84.84\n"
+  "    y: 118.002\n"
+  "    z: 0.0\n"
+  "  }\n"
+  "  rotation {\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
+  "    z: 0.0\n"
+  "    w: 1.0\n"
+  "  }\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"music\"\n"
+  "  type: \"sound\"\n"
+  "  data: \"sound: \\\"/examples/sound/music/bunny_tears.ogg\\\"\\n"
+  "looping: 1\\n"
+  "group: \\\"master\\\"\\n"
+  "gain: 1.0\\n"
+  "\"\n"
+  "  position {\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
+  "    z: 0.0\n"
+  "  }\n"
+  "  rotation {\n"
+  "    x: 0.0\n"
+  "    y: 0.0\n"
+  "    z: 0.0\n"
+  "    w: 1.0\n"
+  "  }\n"
+  "}\n"
+  ""
+  position {
+    x: 335.192
+    y: 316.802
+    z: 0.0
+  }
+  rotation {
+    x: 0.0
+    y: 0.0
+    z: 0.0
+    w: 1.0
+  }
+  scale3 {
+    x: 1.0
+    y: 1.0
+    z: 1.0
+  }
+}

BIN
examples/sound/fade_in_out/fade_in_out.jpg


+ 7 - 0
examples/sound/fade_in_out/fade_in_out.md

@@ -0,0 +1,7 @@
+---
+title: Fade In-Out
+brief: This example shows how to make Fade-In and fade Fade-Out music.
+scripts: fade_in_out.script
+---
+
+![fade_in_out](fade_in_out.jpg)

+ 49 - 0
examples/sound/fade_in_out/fade_in_out.script

@@ -0,0 +1,49 @@
+go.property("gain", 1) -- <1>
+
+local TIME = 2 -- <2>
+local DELAY = 1 -- <3>
+
+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)
+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
+    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
+--]]