【问题标题】:How to make a function wait X amount of time in LUA (Love2d)?如何使函数在 LUA(Love2d)中等待 X 时间?
【发布时间】:2013-03-07 12:37:23
【问题描述】:

我对编程非常陌生,并且来自 SC2 等游戏的“自定义地图”背景。我目前正在尝试在 Love2d 中制作平台游戏。但我想知道如何让某件事在做下一件事之前等待 X 秒。

假设我想让主角长生不死5秒,那代码应该是什么样子?

Immortal = true 
????????????????
Immortal = false

据我了解,Lua 和 Love2d 中没有内置等待。

【问题讨论】:

  • 有一个“睡眠”函数love.timer.sleep(5),但可能最好在函数love.update(dt)内切换Immortal标志

标签: timer lua wait love2d


【解决方案1】:

听起来您对某个游戏实体的临时状态感兴趣。这很常见——通电持续六秒,敌人被击晕两秒,你的角色在跳跃时看起来不同,等等。临时状态与等待不同。等待表明在你的五秒钟不朽期间绝对不会发生任何其他事情。听起来您希望游戏继续正常进行,但要有一个不朽的主角五秒钟。

考虑使用“剩余时间”变量而不是布尔值来表示临时状态。例如:

local protagonist = {
    -- This is the amount of immortality remaining, in seconds
    immortalityRemaining = 0,
    health = 100
}

-- Then, imagine grabbing an immortality powerup somewhere in the game.
-- Simply set immortalityRemaining to the desired length of immortality.
function protagonistGrabbedImmortalityPowerup()
    protagonist.immortalityRemaining = 5
end

-- You then shave off a little bit of the remaining time during each love.update
-- Remember, dt is the time passed since the last update.
function love.update(dt)
    protagonist.immortalityRemaining = protagonist.immortalityRemaining - dt
end

-- When resolving damage to your protagonist, consider immortalityRemaining
function applyDamageToProtagonist(damage)
    if protagonist.immortalityRemaining <= 0 then
        protagonist.health = protagonist.health - damage
    end
end

小心使用 waittimer 等概念。它们通常指的是管理线程。在具有许多移动部件的游戏中,没有线程来管理事物通常更容易且更可预测。如果可能,请将您的游戏视为一个巨大的状态机,而不是在线程之间同步工作。如果线程是绝对必要的,Löve 会在其 love.thread 模块中提供它们。

【讨论】:

    【解决方案2】:

    我通常使用 cron.lua 来处理你所说的:https://github.com/kikito/cron.lua

    Immortal = true
    immortalTimer = cron.after(5, function() 
      Immortal = false 
    end)
    

    然后将immortalTimer:update(dt) 粘贴到您的love.update 中。

    【讨论】:

      【解决方案3】:

      你可以这样做:

      function delay_s(delay)
        delay = delay or 1
        local time_to = os.time() + delay
        while os.time() < time_to do end
      end
      

      那么你可以这样做:

      Immortal == true
      delay_s(5)
      Immortal == false
      

      当然,它会阻止你做任何其他事情,除非你在它自己的线程中运行它。但这完全是 Lua,因为我对 Love2d 一无所知,不幸的是。

      【讨论】:

        【解决方案4】:

        我建议你在游戏中使用 hump.timer,如下所示:

        function love.load()
        timer=require'hump.timer'
        Immortal=true
        timer.after(1,function()
        Immortal=false
        end)
        end
        

        除了使用timer.after,你也可以使用timer.script,像这样:

        function love.load
        timer=require'hump.timer'
        timer.script(function(wait)
        Immortal=true
        wait(5)
        Immortal=false
        end)
        end
        

        不要忘记将timer.update添加到函数love.update中!

        function love.update(dt)
        timer.update(dt)
        end
        

        希望这会有所帮助;)

        下载链接:https://github.com/vrld/hump

        【讨论】:

          猜你喜欢
          • 2022-01-11
          • 2023-03-23
          • 2013-03-17
          • 2022-01-23
          • 2014-01-01
          • 2016-09-19
          • 1970-01-01
          • 2021-03-11
          • 2021-11-23
          相关资源
          最近更新 更多