浏览代码

Merge pull request #8017 from jynus/dodge-the-creeps

Add code examples for sound playing in the "first 2d game" tutorial
Max Hilbrunner 1 年之前
父节点
当前提交
b4fdcc6376
共有 1 个文件被更改,包括 48 次插入1 次删除
  1. 48 1
      getting_started/first_2d_game/07.finishing-up.rst

+ 48 - 1
getting_started/first_2d_game/07.finishing-up.rst

@@ -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
 ~~~~~~~~~~~~~~~~~