How to play sound one time?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
jayzzer
Prole
Posts: 2
Joined: Sat Oct 11, 2014 2:55 pm

How to play sound one time?

Post by jayzzer »

Hello guys, how to play sound one time(in function love.update)?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: How to play sound one time?

Post 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.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
jayzzer
Prole
Posts: 2
Joined: Sat Oct 11, 2014 2:55 pm

Re: How to play sound one time?

Post 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=)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest