【发布时间】:2022-01-08 01:54:53
【问题描述】:
正如标题所示,我想创建一个 love2d 倒计时和秒表。我之前尝试过这样做:
- 倒计时:
function love.load()
timer = 3
end
function love.update(dt)
if timer >= 0 then
timer = timer - dt
else
printMsg = true
end
end
function love.draw()
if printMsg == true then
love.graphics.print('time over')
end
end
- 秒表:
function love.load()
timer = 0
lives = 3
end
function love.update(dt)
if lives <= 0 then
timer = timer + dt
else
printMsg = true
end
end
function love.draw()
if printMsg == true then
love.graphics.print('you have survived for '..timer..' seconds')
end
end
function love.mousepressed(x, y, button)
if button == 1 then --if left mouse button is pressed then 1 is subtracted from lives
lives = lives - 1
end
end
但问题是dt 不是一个常数,因此定时器的增加或减少甚至不会导致倒计时或秒表值更快或更慢(主要是更快)。
我也尝试使用love.timer.getTime(),但它仍然与dt 相同。非常感谢任何帮助。提前感谢任何答案。
【问题讨论】:
-
注意:它的速度非常快。只有在后台运行密集型软件时才会变慢。
-
love.graphics是一个库,而不是回调。你的意思是love.draw? -
我的错,现在改正了
-
我不明白你的问题。请详细说明为什么您认为不能使用 dt 进行计时。这是它的唯一目的。你在那里打印的
time值是什么?它来自哪里?