Skip to content

Commit 3f45d36

Browse files
committed
Add code examples for sound playing in the "first 2d game" tutorial
The last step in "Your first 2D game" (Finishing up) lacks code or directions in general for C#. Add a short snipet that can be used for reference and for copy and paste. Fixes #8003
1 parent fed201e commit 3f45d36

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

getting_started/first_2d_game/07.finishing-up.rst

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,60 @@ audio file.
3535

3636
All audio is automatically imported with the ``Loop`` setting disabled.
3737
If you want the music to loop seamlessly, click on the Stream file arrow,
38-
select ``Make Unique``, then click on the Stream file and check the ``Loop`` box.
38+
select ``Make Unique``, then click on the Stream file and check the ``Loop`` box.
3939

4040
To play the music, add ``$Music.play()`` in the ``new_game()``
4141
function and ``$Music.stop()`` in the ``game_over()`` function.
4242

4343
Finally, add ``$DeathSound.play()`` in the ``game_over()`` function.
4444

45+
.. tabs::
46+
.. code-tab:: gdscript GDScript
47+
48+
func game_over():
49+
...
50+
$Music.stop()
51+
$DeathSound.play()
52+
53+
func new_game():
54+
...
55+
$Music.play()
56+
57+
.. code-tab:: csharp
58+
59+
public void GameOver()
60+
{
61+
...
62+
GetNode<AudioStreamPlayer>("Music").Stop();
63+
GetNode<AudioStreamPlayer>("DeathSound").Play();
64+
}
65+
66+
public void NewGame()
67+
{
68+
...
69+
GetNode<AudioStreamPlayer>("Music").Play();
70+
}
71+
72+
.. code-tab:: cpp
73+
74+
void Main::_ready() {
75+
...
76+
_music = get_node<godot::AudioStreamPlayer>("Music");
77+
_death_sound = get_node<godot::AudioStreamPlayer>("DeathSound");
78+
}
79+
80+
void Main::game_over() {
81+
...
82+
_music->stop();
83+
_death_sound->play();
84+
}
85+
86+
void Main::new_game() {
87+
...
88+
_music->play();
89+
}
90+
91+
4592
Keyboard shortcut
4693
~~~~~~~~~~~~~~~~~
4794

0 commit comments

Comments
 (0)