Explorar el Código

Added sound get and set example

Björn Ritzl hace 2 meses
padre
commit
5d738675c8

+ 18 - 0
sound/get_set_sound/all.texture_profiles

@@ -0,0 +1,18 @@
+path_settings {
+  path: "**"
+  profile: "Default"
+}
+profiles {
+  name: "Default"
+  platforms {
+    os: OS_ID_GENERIC
+    formats {
+      format: TEXTURE_FORMAT_RGBA
+      compression_level: BEST
+      compression_type: COMPRESSION_TYPE_DEFAULT
+    }
+    mipmaps: false
+    max_texture_size: 0
+    premultiply_alpha: true
+  }
+}

BIN
sound/get_set_sound/assets/SourceSansPro-Semibold.ttf


BIN
sound/get_set_sound/assets/images/shipGreen_manned.png


+ 4 - 0
sound/get_set_sound/assets/sprites.atlas

@@ -0,0 +1,4 @@
+images {
+  image: "/assets/images/shipGreen_manned.png"
+}
+extrude_borders: 2

+ 5 - 0
sound/get_set_sound/assets/text48.font

@@ -0,0 +1,5 @@
+font: "/assets/SourceSansPro-Semibold.ttf"
+material: "/builtins/fonts/font.material"
+size: 48
+outline_alpha: 0.0
+outline_width: 0.0

+ 15 - 0
sound/get_set_sound/example.md

@@ -0,0 +1,15 @@
+---
+tags: sound,resource
+title: Get and set sound
+brief: This example shows how to change which sound a sound component plays
+author: Defold
+scripts: get_set_sound.script
+---
+
+This example shows how to change which sound a sound component plays. Additional sounds are stored as individual .ogg files in the `sounds` folder and included in the build as [Custom Resources](https://defold.com/manuals/file-access/#custom-resources):
+
+![](game_project.png)
+
+The example consists of a single collection with a game object containing a Sound component, a Sprite component for visuals and a Script to control the logic:
+
+![](get_set_sound_collection.png)

+ 32 - 0
sound/get_set_sound/example/get_set_sound.collection

@@ -0,0 +1,32 @@
+name: "default"
+scale_along_z: 0
+embedded_instances {
+  id: "gameobject"
+  data: "components {\n"
+  "  id: \"script\"\n"
+  "  component: \"/example/get_set_sound.script\"\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"sprite\"\n"
+  "  type: \"sprite\"\n"
+  "  data: \"default_animation: \\\"shipGreen_manned\\\"\\n"
+  "material: \\\"/builtins/materials/sprite.material\\\"\\n"
+  "textures {\\n"
+  "  sampler: \\\"texture_sampler\\\"\\n"
+  "  texture: \\\"/assets/sprites.atlas\\\"\\n"
+  "}\\n"
+  "\"\n"
+  "}\n"
+  "embedded_components {\n"
+  "  id: \"enginesound\"\n"
+  "  type: \"sound\"\n"
+  "  data: \"sound: \\\"/sounds/spaceEngine_000.ogg\\\"\\n"
+  "looping: 1\\n"
+  "\"\n"
+  "}\n"
+  ""
+  position {
+    x: 335.192
+    y: 316.802
+  }
+}

+ 32 - 0
sound/get_set_sound/example/get_set_sound.script

@@ -0,0 +1,32 @@
+function init(self)
+    msg.post(".", "acquire_input_focus")
+
+    -- animate the spaceship up and down "for dramatic effect"
+    go.animate(".", "position.y", go.PLAYBACK_LOOP_PINGPONG, go.get_position().y + 20, go.EASING_INOUTQUAD, 1)
+
+    -- play the engine sound
+    sound.play("#enginesound")
+end
+
+
+function on_input(self, action_id, action)
+    if action_id == hash("mouse_button_left") and action.pressed then
+        -- a list of sounds to chose between
+        local sounds = {
+            "/sounds/spaceEngine_001.ogg",
+            "/sounds/spaceEngine_002.ogg",
+            "/sounds/spaceEngine_003.ogg",
+        }
+        -- pick one at random
+        local random_sound = sounds[math.random(1, #sounds)]
+
+        -- load the new sound
+        -- stop the currently playing sound
+        -- set the sound on the sound component
+        -- play it again
+        local engine3 = sys.load_resource(random_sound)
+        sound.stop("#enginesound")
+        resource.set_sound(go.get("#enginesound", "sound"), engine3)
+        sound.play("#enginesound")
+    end
+end

+ 66 - 0
sound/get_set_sound/game.project

@@ -0,0 +1,66 @@
+[project]
+title = Get and Set Sound
+version = 0.1
+custom_resources = sounds
+
+[bootstrap]
+main_collection = /example/get_set_sound.collectionc
+
+[input]
+game_binding = /builtins/input/all.input_bindingc
+repeat_interval = 0.05
+
+[display]
+width = 720
+height = 720
+high_dpi = 1
+
+[physics]
+scale = 0.02
+gravity_y = -500.0
+
+[script]
+shared_state = 1
+
+[collection_proxy]
+max_count = 256
+
+[label]
+subpixels = 1
+
+[sprite]
+subpixels = 1
+max_count = 32765
+
+[windows]
+iap_provider = 
+
+[android]
+package = com.defold.examples
+
+[ios]
+bundle_identifier = com.defold.examples
+
+[osx]
+bundle_identifier = com.defold.examples
+
+[html5]
+show_fullscreen_button = 0
+show_made_with_defold = 0
+scale_mode = no_scale
+heap_size = 64
+
+[graphics]
+texture_profiles = /all.texture_profiles
+
+[collection]
+max_instances = 32765
+
+[particle_fx]
+max_emitter_count = 1024
+
+[render]
+clear_color_blue = 1.0
+clear_color_green = 1.0
+clear_color_red = 1.0
+

BIN
sound/get_set_sound/game_project.png


BIN
sound/get_set_sound/get_set_sound_collection.png


BIN
sound/get_set_sound/sounds/spaceEngine_000.ogg


BIN
sound/get_set_sound/sounds/spaceEngine_001.ogg


BIN
sound/get_set_sound/sounds/spaceEngine_002.ogg


BIN
sound/get_set_sound/sounds/spaceEngine_003.ogg