Increasing a counter on sound:isStopped()

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
NÖÖB
Prole
Posts: 41
Joined: Thu Jul 31, 2008 10:57 pm
Location: Norway

Increasing a counter on sound:isStopped()

Post by NÖÖB »

Hey guys, I need a little help with this; when the sound (you can't hear it) ends, I want the counter to increase by 1 when the sound plays again, but most of the time it increases by 2, 3, and I can't see why..

Code: Select all

function love.load()
	oldcounter = 0
	counter = 0
	
	local samples = math.floor(1 * 44100) -- create sound; 1 second silence
	local data = love.sound.newSoundData(samples, 44100, 16, 1)
	for i = 0,samples do data:setSample(i, 0) end
	silence = love.audio.newSource(data)
	love.audio.play(silence)
end

function love.update(dt)
	if silence:isStopped() then
		love.audio.play(silence)
		counter = counter + 1
	end
end

function love.draw()
	love.graphics.print(counter, 1, 100)
end
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: Increasing a counter on sound:isStopped()

Post by leiradel »

My guess is that there is some latency between love.audio.play( silence ) and silence:isStopped() but I'm not sure.

Anyway, if what you're trying to accomplish is to increment a counter every one second, this code will do it (untested):

Code: Select all

local one_second
local count

function love.load()
  one_second = 0
  count = 0
end

function love.update( dt )
  one_second = one_second + dt
  if one_second >= 1 then
    one_second = one_second - 1
    count = count + 1
  end
end
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests