Skip to content

Commit 5d73867

Browse files
committed
Added sound get and set example
1 parent 97a83ae commit 5d73867

15 files changed

+172
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
path_settings {
2+
path: "**"
3+
profile: "Default"
4+
}
5+
profiles {
6+
name: "Default"
7+
platforms {
8+
os: OS_ID_GENERIC
9+
formats {
10+
format: TEXTURE_FORMAT_RGBA
11+
compression_level: BEST
12+
compression_type: COMPRESSION_TYPE_DEFAULT
13+
}
14+
mipmaps: false
15+
max_texture_size: 0
16+
premultiply_alpha: true
17+
}
18+
}
146 KB
Binary file not shown.
4.07 KB
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
images {
2+
image: "/assets/images/shipGreen_manned.png"
3+
}
4+
extrude_borders: 2
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
font: "/assets/SourceSansPro-Semibold.ttf"
2+
material: "/builtins/fonts/font.material"
3+
size: 48
4+
outline_alpha: 0.0
5+
outline_width: 0.0

sound/get_set_sound/example.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
tags: sound,resource
3+
title: Get and set sound
4+
brief: This example shows how to change which sound a sound component plays
5+
author: Defold
6+
scripts: get_set_sound.script
7+
---
8+
9+
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):
10+
11+
![](game_project.png)
12+
13+
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:
14+
15+
![](get_set_sound_collection.png)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: "default"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "gameobject"
5+
data: "components {\n"
6+
" id: \"script\"\n"
7+
" component: \"/example/get_set_sound.script\"\n"
8+
"}\n"
9+
"embedded_components {\n"
10+
" id: \"sprite\"\n"
11+
" type: \"sprite\"\n"
12+
" data: \"default_animation: \\\"shipGreen_manned\\\"\\n"
13+
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
14+
"textures {\\n"
15+
" sampler: \\\"texture_sampler\\\"\\n"
16+
" texture: \\\"/assets/sprites.atlas\\\"\\n"
17+
"}\\n"
18+
"\"\n"
19+
"}\n"
20+
"embedded_components {\n"
21+
" id: \"enginesound\"\n"
22+
" type: \"sound\"\n"
23+
" data: \"sound: \\\"/sounds/spaceEngine_000.ogg\\\"\\n"
24+
"looping: 1\\n"
25+
"\"\n"
26+
"}\n"
27+
""
28+
position {
29+
x: 335.192
30+
y: 316.802
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function init(self)
2+
msg.post(".", "acquire_input_focus")
3+
4+
-- animate the spaceship up and down "for dramatic effect"
5+
go.animate(".", "position.y", go.PLAYBACK_LOOP_PINGPONG, go.get_position().y + 20, go.EASING_INOUTQUAD, 1)
6+
7+
-- play the engine sound
8+
sound.play("#enginesound")
9+
end
10+
11+
12+
function on_input(self, action_id, action)
13+
if action_id == hash("mouse_button_left") and action.pressed then
14+
-- a list of sounds to chose between
15+
local sounds = {
16+
"/sounds/spaceEngine_001.ogg",
17+
"/sounds/spaceEngine_002.ogg",
18+
"/sounds/spaceEngine_003.ogg",
19+
}
20+
-- pick one at random
21+
local random_sound = sounds[math.random(1, #sounds)]
22+
23+
-- load the new sound
24+
-- stop the currently playing sound
25+
-- set the sound on the sound component
26+
-- play it again
27+
local engine3 = sys.load_resource(random_sound)
28+
sound.stop("#enginesound")
29+
resource.set_sound(go.get("#enginesound", "sound"), engine3)
30+
sound.play("#enginesound")
31+
end
32+
end

sound/get_set_sound/game.project

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[project]
2+
title = Get and Set Sound
3+
version = 0.1
4+
custom_resources = sounds
5+
6+
[bootstrap]
7+
main_collection = /example/get_set_sound.collectionc
8+
9+
[input]
10+
game_binding = /builtins/input/all.input_bindingc
11+
repeat_interval = 0.05
12+
13+
[display]
14+
width = 720
15+
height = 720
16+
high_dpi = 1
17+
18+
[physics]
19+
scale = 0.02
20+
gravity_y = -500.0
21+
22+
[script]
23+
shared_state = 1
24+
25+
[collection_proxy]
26+
max_count = 256
27+
28+
[label]
29+
subpixels = 1
30+
31+
[sprite]
32+
subpixels = 1
33+
max_count = 32765
34+
35+
[windows]
36+
iap_provider =
37+
38+
[android]
39+
package = com.defold.examples
40+
41+
[ios]
42+
bundle_identifier = com.defold.examples
43+
44+
[osx]
45+
bundle_identifier = com.defold.examples
46+
47+
[html5]
48+
show_fullscreen_button = 0
49+
show_made_with_defold = 0
50+
scale_mode = no_scale
51+
heap_size = 64
52+
53+
[graphics]
54+
texture_profiles = /all.texture_profiles
55+
56+
[collection]
57+
max_instances = 32765
58+
59+
[particle_fx]
60+
max_emitter_count = 1024
61+
62+
[render]
63+
clear_color_blue = 1.0
64+
clear_color_green = 1.0
65+
clear_color_red = 1.0
66+
74.9 KB
Loading

0 commit comments

Comments
 (0)