ソースを参照

Updated sound effects Lua sample to use SetAutoRemoveMode() instead of nodes.

Enhex 8 年 前
コミット
0e2182b4e8
1 ファイル変更14 行追加28 行削除
  1. 14 28
      bin/Data/LuaScripts/14_SoundEffects.lua

+ 14 - 28
bin/Data/LuaScripts/14_SoundEffects.lua

@@ -17,10 +17,20 @@ local soundResourceNames = {
     "Sounds/Powerup.wav"
     }
 
+local musicSource
+
 function Start()
     -- Execute the common startup for samples
     SampleStart()
 
+    -- Create a scene which will not be actually rendered, but is used to hold SoundSource components while they play sounds
+    scene_ = Scene()
+
+    -- Create music sound source
+    musicSource = scene_:CreateComponent("SoundSource")
+    -- Set the sound type to music so that master volume control works correctly
+    musicSource.soundType = SOUND_MUSIC
+
     -- Enable OS cursor
     input.mouseVisible = true
 
@@ -32,8 +42,6 @@ function Start()
 end
 
 function CreateUI()
-    -- Create a scene which will not be actually rendered, but is used to hold SoundSource components while they play sounds
-    scene_ = Scene()
 
     local uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
     -- Set style to the UI root so that elements will inherit it
@@ -108,44 +116,27 @@ function HandlePlaySound(sender, eventType, eventData)
     local sound = cache:GetResource("Sound", soundResourceName)
 
     if sound ~= nil then
-        -- Create a scene node with a SoundSource component for playing the sound. The SoundSource component plays
+        -- Create a SoundSource component for playing the sound. The SoundSource component plays
         -- non-positional audio, so its 3D position in the scene does not matter. For positional sounds the
         -- SoundSource3D component would be used instead
-        local soundNode = scene_:CreateChild("Sound")
-        local soundSource = soundNode:CreateComponent("SoundSource")
+        local soundSource = scene_:CreateComponent("SoundSource")
+        soundSource:SetAutoRemoveMode(REMOVE_COMPONENT)
         soundSource:Play(sound)
         -- In case we also play music, set the sound volume below maximum so that we don't clip the output
         soundSource.gain = 0.7
-
-        -- Subscribe to the "sound finished" event generated by the SoundSource for removing the node once the sound has played
-        -- Note: the event is sent through the Node (similar to e.g. node physics collision and animation trigger events)
-        -- to not require subscribing to the particular component
-        SubscribeToEvent(soundNode, "SoundFinished", "HandleSoundFinished");
     end
 end
 
 function HandlePlayMusic(eventType, eventData)
-
-    -- Check if the music player node/component already exist
-    if scene_:GetChild("Music") ~= nil then
-        return
-    end
-
     local music = cache:GetResource("Sound", "Music/Ninja Gods.ogg")
     -- Set the song to loop
     music.looped = true
 
-    -- Create a scene node and a sound source for the music
-    local musicNode = scene_:CreateChild("Music")
-    local musicSource = musicNode:CreateComponent("SoundSource")
-    -- Set the sound type to music so that master volume control works correctly
-    musicSource.soundType = SOUND_MUSIC
     musicSource:Play(music)
 end
 
 function HandleStopMusic(eventType, eventData)
-    -- Remove the music player node from the scene
-    scene_:RemoveChild(scene_:GetChild("Music"))
+    musicSource:Stop()
 end
 
 function HandleSoundVolume(eventType, eventData)
@@ -158,11 +149,6 @@ function HandleMusicVolume(eventType, eventData)
     audio:SetMasterGain(SOUND_MUSIC, newVolume)
 end
 
-function HandleSoundFinished(eventType, eventData)
-    local soundNode = eventData["Node"]:GetPtr("Node")
-    soundNode:Remove()
-end
-
 -- Create XML patch instructions for screen joystick layout specific to this sample app
 function GetScreenJoystickPatchString()
     return