Topic: Play sound once

Hi,
I am having problems getting a sound to play once in my script.
At the start:
soundplay = 0
   function playsound()
       if soundplay == 0 then playSound(Sound0) end
       soundplay = 1
   end
Then:
function onSceneUpdate()
if isCollisionBetween(Car, Ground)
if soundplay == 0 then playsound()
else
soundplay = 0
end
end

It loops repeatedly, but I'm not sure why, could anyone help?

Re: Play sound once

I used that method into a test scene, maybe that's what you are looking for :

Bleep = getObject("Bleep")  -- the sound file
SOUND = {bleep = 0}

function onSceneUpdate()
    if isCollisionBetween(object1, object2) then
        play_Bleep()
    else
        stop_Bleep()   
    end
end
   
function play_Bleep()
    if SOUND.bleep == 0 then playSound(Bleep) end
       SOUND.bleep = 1
end

function stop_Bleep()
    SOUND.bleep = 0
end

Re: Play sound once

Ok, I now have:

Sound0 = getObject("Sound0")
IMPACT = {Sound0 = 0}
   function play_impact()
       if IMPACT.Sound0 == 0 then playSound(Sound0) end
       IMPACT.Sound0 = 1
   end
   function stop_impact()
   IMPACT.Sound0 = 0
   end
function onSceneUpdate()
if isCollisionBetween(Car, Ground)
then changeCurrentCamera(Camera1)
deactivate(Text0)
deactivate(Text1)
camcycle = 1
if IMPACT.Sound0 == 0 then play_impact()
else
stop_impact()
end
end

But it still loops. Any idea why?

Last edited by Almighty Laxz (2012-09-13 10:14:51)

Re: Play sound once

Try this :

Sound0 = getObject("Sound0")

SOUND = {impact = 0}

function onSceneUpdate()
    if isCollisionBetween(Car, Ground) then
        changeCurrentCamera(Camera1)
        deactivate(Text0)
        deactivate(Text1)
        camcycle = 1
        play_impact()
    else
        stop_impact()
    end   
end

function play_impact()
    if SOUND.impact == 0 then playSound(Sound0) end
       SOUND.impact = 1
end
   
function stop_impact()
    SOUND.impact = 0
end

Re: Play sound once

Whoops, it actually didn't work, its still looping.
I now have:

Sound0 = getObject("Sound0")
SOUND = {impact = 0}
function onSceneUpdate()
   function play_impact()
    if SOUND.impact == 0 then playSound(Sound0) end
       SOUND.impact = 1
   end
   function stop_impact()
   SOUND.impact = 0
   end
if isCollisionBetween(Car, Ground)
then changeCurrentCamera(Camera1)
deactivate(Text0)
deactivate(Text1)
camcycle = 1
play_impact()
else
stop_impact()
end

Putting the functions after isCollisionBetween doesn't work, it stops the script working.

Re: Play sound once

And something like that ?

Sound0 = getObject("Sound0")
SOUND = {obj = Sound0, impact = 0}

function play_impact(sound)
    if sound.impact == 0 then
        playSound(sound.obj)
        sound.impact = 1
    end
end

function stop_impact(sound)
   sound.impact = 0
end

function onSceneUpdate()
   
    if isCollisionBetween(Car, Ground) then
        changeCurrentCamera(Camera1)
        deactivate(Text0)
        deactivate(Text1)  
        camcycle = 1
        play_impact(SOUND)
    else
        stop_impact(SOUND)
    end

end

Re: Play sound once

Still not working, I now have:

Sound0 = getObject("Sound0")
SOUND = {obj = Sound0, impact = 0}
   function play_impact(sound)
    if SOUND.impact == 0 then playSound(sound.obj) end
       SOUND.impact = 1
   end
   function stop_impact(sound)
   SOUND.impact = 0
   end
function onSceneUpdate()
if isCollisionBetween(Car, Ground)
then changeCurrentCamera(Camera1)
deactivate(Text0)
deactivate(Text1)
camcycle = 1
play_impact(SOUND)
else
stop_impact(SOUND)
end

Any other ideas?

Re: Play sound once

Well, I was hoping to find my answer here, guess I have to try elsewhere.

Re: Play sound once

I had to revisit this post, as I need this again, however, I am on a mac now and something as simple as:

if isCollisionBetween(box,trigger) then
        playSound(news)
    end

In the update function plays the sound only once (although it plays it after the collision is finished, not upon impact).

I wonder if the issue is a Windows thing? Or at least, I don't hear the repetitions if there are any.

Nope. It still skips

Last edited by Tutorial Doctor (2014-12-16 20:40:19)

Re: Play sound once

Hey TD, the scripts Anael & i posted are both valid, i assume Almighty Laxz problem was somewhere else
Here's a project with both scripts (you must un-comment one of the method first) :
https://dl.dropboxusercontent.com/u/199 … ndtest.rar

Re: Play sound once

Thanks Vegas. I'll download them today and test. How are you By the way?

Re: Play sound once

Fine thanks ! just finished a 4 months pre-forming module,
now i'm going to hibernate for a week before starting something else wink