【问题标题】:Frame independent time in Lua?在Lua中构建独立时间?
【发布时间】:2011-08-10 17:47:25
【问题描述】:

我正在使用 Love2D 为我和我的朋友创建一个小游戏,但是我遇到了一个问题: 我想计算经过的时间,与帧率无关。我正在尝试这个,但会出现轻微的错误,最终“秒”会在 1/100 秒内通过。

local last_time = os.time()

function timeofday_update()
    world_time = world_time + os.time() - last_time
end

【问题讨论】:

  • 你能解释一下这段代码应该完成什么以及它是如何失败的吗?

标签: time timer lua frame-rate love2d


【解决方案1】:

为什么不在程序开始时标记时间,或者何时开始(starting_time = os.time()),然后'当前经过的时间'就是os.time() - 开始时间。无需积累...

【讨论】:

    【解决方案2】:
    function make_stopwatch ()
        local start = 0
        local finish = 0
        local function sw (cmd)
            if cmd == "start" then
                start = os.time()
                return 0
            end
            if cmd == "lap" then
                return os.difftime(os.time(), start)
            end
            if cmd == "stop" then
                finish = os.time()
            end
            return os.difftime(finish, start)
        end
        return sw 
    end
    

    演示:

    > sw = make_stopwatch()
    > =sw("start")
    0
    > =sw("lap")
    16
    > =sw "lap"
    22
    > =sw "lap"
    28
    > =sw "stop"
    42
    > = sw()
    42
    > = sw()
    42
    > = sw "start"
    0
    > = sw "lap"
    8
    > 
    

    【讨论】:

      猜你喜欢
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多