PlaySound

From MaratisWiki
(Difference between revisions)
Jump to: navigation, search
m
 
Line 5: Line 5:
 
{{lua-function-param|object|object to operate on}}
 
{{lua-function-param|object|object to operate on}}
  
'''Note:''' If you enable "loop" in the sound object proprieties, the sound will play even if there's no script applied to it.
+
 
 +
* The '''Loop''' option will play and loop the sound even if there's no script applied to it.
 +
* If the '''Relative''' option is checked, the sound will play on 'global' mode (won't be affected by position)
 +
 
  
 
'''Example 1 :''' Play a sound on key press. will work fine only with onKeyDown. if you try with isKeyPressed, sound will be played 1 time every frames, so you must use example 2 or 3 method instead.
 
'''Example 1 :''' Play a sound on key press. will work fine only with onKeyDown. if you try with isKeyPressed, sound will be played 1 time every frames, so you must use example 2 or 3 method instead.
  
    Sound0 = getObject("Sound0")
+
<nowiki>Sound0 = getObject("Sound0")
    function onSceneUpdate()
+
function onSceneUpdate()
        if onKeyDown("SPACE") then playSound(Sound0) end  
+
    if onKeyDown("SPACE") then playSound(Sound0) end  
    end
+
end</nowiki>
  
  
 
'''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")
+
<nowiki>Sound0 = getObject("Sound0")
    soundplay = 0
+
soundplay = 0
    function onSceneUpdate()
+
function onSceneUpdate()
        if isKeyPressed("SPACE") then  
+
    if isKeyPressed("SPACE") then  
            if soundplay == 0 then playSound(Sound0) end
+
        if soundplay == 0 then playSound(Sound0) end
            soundplay = 1  
+
        soundplay = 1  
        else
+
    else
            soundplay = 0
+
        soundplay = 0
        end
+
 
     end
 
     end
 +
end</nowiki>
  
  
 
'''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")
+
<nowiki>Sound0 = getObject("Sound0")
 +
soundplay = 0
 +
 
 +
function playmysound()
 +
    if soundplay == 0 then playSound(Sound0) end
 +
    soundplay = 1
 +
end
 +
 
 +
function stopmysound()
 
     soundplay = 0
 
     soundplay = 0
    function playmysound()
+
end
        if soundplay == 0 then playSound(Sound0) end
+
 
        soundplay = 1
+
function onSceneUpdate()
    end
+
     if isKeyPressed("SPACE") then  
    function stopmysound()
+
        playmysound()  
        soundplay = 0
+
    else
     end
+
        stopmysound()
    function onSceneUpdate()
+
        if isKeyPressed("SPACE") then  
+
            playmysound()  
+
        else
+
            stopmysound()
+
        end
+
 
     end
 
     end
 +
end</nowiki>
  
 
[[Category:Lua scripting]]
 
[[Category:Lua scripting]]
 
[[Category:Lua function]]
 
[[Category:Lua function]]
 
[[Category:Sound]]
 
[[Category:Sound]]

Latest revision as of 16:00, 15 February 2014

playSound(object)

Play a sound.

  • object : object to operate on


  • The Loop option will play and loop the sound even if there's no script applied to it.
  • If the Relative option is checked, the sound will play on 'global' mode (won't be affected by position)


Example 1 : Play a sound on key press. will work fine only with onKeyDown. if you try with isKeyPressed, sound will be played 1 time every frames, so you must use example 2 or 3 method instead.

Sound0 = getObject("Sound0")
function onSceneUpdate()
    if onKeyDown("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
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox