Page 1 of 1

How to play sound one time?

Posted: Sat Oct 11, 2014 3:01 pm
by jayzzer
Hello guys, how to play sound one time(in function love.update)?

Re: How to play sound one time?

Posted: Sat Oct 11, 2014 3:05 pm
by Nixola
You can trigger the play function just once, maybe using a boolean:

Code: Select all

local sound = love.audio.newSource(filename)
local play = true

love.update = function(dt)
   if play then
      sound:play()
      play = false
   end
end
This will load the sound and create a variable once. Then, every frame, it checks if the variable is true: if so, it plays the sound and sets it to false, so that it doesn't happen again.

Re: How to play sound one time?

Posted: Sat Oct 11, 2014 4:28 pm
by jayzzer
Nixola wrote:You can trigger the play function just once, maybe using a boolean:

Code: Select all

local sound = love.audio.newSource(filename)
local play = true

love.update = function(dt)
   if play then
      sound:play()
      play = false
   end
end
This will load the sound and create a variable once. Then, every frame, it checks if the variable is true: if so, it plays the sound and sets it to false, so that it doesn't happen again.
Ok, Thanks=)