PlaySound
(Difference between revisions)
Dahnielson (Talk | contribs) |
Dahnielson (Talk | contribs) |
||
Line 1: | Line 1: | ||
− | + | {{lua-function|playSound|object}} | |
− | + | Play a sound. | |
− | Example 1 (WRONG example) : This is probably what you will try. But this won't work as expected because the sound will | + | '''Note:''' If you enable "loop" in the sound object proprieties, the sound will play even if there's no script applied to it. |
+ | |||
+ | '''Example 1 (WRONG example):''' This is probably what you will try. But this won't work as expected because the sound will | ||
reinitialize all the time while you press SPACE. | reinitialize all the time while you press SPACE. | ||
Line 11: | Line 13: | ||
end | end | ||
− | Example 2 : Play a sound on key press. the sound will stop playing after 1 time | + | '''Example 2:''' Play a sound on key press. the sound will stop playing after 1 time |
Sound0 = getObject("Sound0") | Sound0 = getObject("Sound0") | ||
Line 24: | Line 26: | ||
end | end | ||
− | Example 3 : Same example as above, but used with functions (for easy re-using) | + | '''Example 3:''' Same example as above, but used with functions (for easy re-using) |
Sound0 = getObject("Sound0") | Sound0 = getObject("Sound0") |
Revision as of 17:03, 8 February 2014
playSound(object)
Play a sound.
Note: If you enable "loop" in the sound object proprieties, the sound will play even if there's no script applied to it.
Example 1 (WRONG example): This is probably what you will try. But this won't work as expected because the sound will reinitialize all the time while you press SPACE.
Sound0 = getObject("Sound0") function onSceneUpdate() if isKeyPressed("SPACE") then playSound(Sound0) end end
Example 2: Play a sound on key press. the sound will stop playing after 1 time
Sound0 = getObject("Sound0") soundplay = 0 function onSceneUpdate() if isKeyPressed("SPACE") then if soundplay == 0 then playSound(Sound0) end soundplay = 1 else soundplay = 0 end end
Example 3: Same example as above, but used with functions (for easy re-using)
Sound0 = getObject("Sound0") soundplay = 0 function playmysound() if soundplay == 0 then playSound(Sound0) end soundplay = 1 end function stopmysound() soundplay = 0 end function onSceneUpdate() if isKeyPressed("SPACE") then playmysound() else stopmysound() end end