Love2d 倒计时和计时器

就像标题所说,我想创建一个Love2d倒计时和一个计时器。 我之前尝试按照以下方式实现:

  1. 倒计时:
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('时间到')
   end
end
  1. 计时器:
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('你存活了 '..timer..' 秒')
   end
end

function love.mousepressed(x, y, button)
   if button == 1 then  --如果按下鼠标左键,则将1从生命中减去
      lives = lives - 1
   end
end

但问题在于 dt 不是一个常数,因此计时器增加或减少并不会导致倒计时或计时器值变得更快或更慢(大多数情况下更快)。同时,我还尝试使用 love.timer.getTime(),但结果与 dt 相同。 如果有帮助,将不胜感激。 预先感谢您的任何答案。

原文链接 https://stackoverflow.com/questions/70184801

点赞
stackoverflow用户17783369
stackoverflow用户17783369

尝试:

function love.update(dt)
    cffcgf = cffcgf + dt -- 我为此用头猛撞了键盘
    if timer >= 0 then
        if cffcgf >= 1 then -- 检查一秒钟是否过去
            cffcgf = 0
            timer = timer - 1
        end
    else
        printMsg = true
    end
end

尝试使用同样的方法来用于秒表。

2022-04-30 23:06:10