Page 1 of 1

Increasing a counter on sound:isStopped()

Posted: Tue Mar 29, 2011 8:57 pm
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

Re: Increasing a counter on sound:isStopped()

Posted: Tue Mar 29, 2011 11:25 pm
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