Skip to content

Commit

Permalink
Add code examples for sound playing in the "first 2d game" tutorial
Browse files Browse the repository at this point in the history
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 godotengine#8003
  • Loading branch information
jynus committed Sep 23, 2023
1 parent fed201e commit 3f45d36
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion getting_started/first_2d_game/07.finishing-up.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,60 @@ audio file.

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

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

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

.. tabs::
.. code-tab:: gdscript GDScript

func game_over():
...
$Music.stop()
$DeathSound.play()

func new_game():
...
$Music.play()

.. code-tab:: csharp

public void GameOver()
{
...
GetNode<AudioStreamPlayer>("Music").Stop();
GetNode<AudioStreamPlayer>("DeathSound").Play();
}

public void NewGame()
{
...
GetNode<AudioStreamPlayer>("Music").Play();
}

.. code-tab:: cpp

void Main::_ready() {
...
_music = get_node<godot::AudioStreamPlayer>("Music");
_death_sound = get_node<godot::AudioStreamPlayer>("DeathSound");
}

void Main::game_over() {
...
_music->stop();
_death_sound->play();
}

void Main::new_game() {
...
_music->play();
}


Keyboard shortcut
~~~~~~~~~~~~~~~~~

Expand Down

0 comments on commit 3f45d36

Please sign in to comment.