-- in my update callback
function love.update(dt)
if paused == true then return
else
-- run the code ..
-- and somewhere in my update function, I also have
if start_time == true then
time = time + dt
else
time = time + 0
end
end
end
-- in my focus callback
function love.focus(f)
if not f then
paused = true
start_time = false
else
paused = false
start_time = true
end
end
And the result, my game successfully stop running and starts freezing when it gets paused. But when I'm trying to print the variable 'time' in the screen, the time keeps running and not freezing even the game paused.
Also when I press home button or back button, the time stills running in the game.
pgimeno wrote: ↑Mon Jul 01, 2019 10:42 am
There must be something else we don't know. At first sight that should stop counting the time. Have you tried printing start_time and paused?
Yes.The start_time printed true and the paused printed false in the screen. Then when I want to slide down my notification bar and put it back again, both start_time and paused still prints the same value. But I don't have idea how to see their value when I'm not in focus, also in my IDE I dont have something like console to print() it in. Anyway, the timer changed so far when I'm back to the screen after not focusing. For example, the timer is in 2 seconds first, then after not focusing in some seconds and back again, it turned to 10 seconds or maybe 12 or 13 etc. not continuing the last second (which is 2).
function love.load()
time = 0
time_starts = true
text = ""
x = 0
starts_moving = true
end
function love.update(dt)
if time_starts == true then
time = time + dt
else
time = time + 0
end
if starts_moving == true then
x = x + 1
if x > love.graphics.getWidth() then
x = 0
end
else
x = x + 0
end
end
function love.draw()
love.graphics.print(time)
love.graphics.print(text, 0, 100)
love.graphics.rectangle("fill", x, 20, 60, 60)
love.graphics.print('x: ' .. x, 0, 200)
end
function love.focus(f)
if not f then
time_starts = false
starts_moving = false
text = "uh-oh why the timer didn't stops when not focusing?"
else
starts_moving = true
time_starts = true
end
end
So, in light of what slime has noted, the easiest fix would be to limit dt to, say, 0.1s. That would advance the clock by at most 0.1 seconds during a pause.
If that's still not desirable, well, if the love.focus() event executes, it can set a flag that would tell the love.update() event to skip that frame.