如何在LOVE2D中等待条件的满足?

我尝试过

timer.script(function(wait)
repeat
    wait(0)
until condiction
end)

但它没有工作。请帮我!

点赞
用户194758
用户194758

timer.script 并不是为你正在尝试做的事情而设计的,不过它 可能 可以工作。

LÖVE 框架是围绕 draw()update() 回调建立的,我建议先学习如何使用这些回调完成此任务,然后再使用建立在这些回调上的方法。像这样,当第一次满足条件时,你的代码将仅运行一次:

local hasHappened = false
function love.update(dt)
  if (condition and not hasHappened) then
    hasHappened = true
    -- respond to condition here
  end
end

通常情况下,你不会直接在 love.update() 中检查条件。相反,你会拥有一个包含所有游戏对象的表格,在 love.update() 中循环该表格并对每个对象调用 update() 方法。这样每个对象都有机会检查不同的条件并对其作出响应。

另一种方法是给你的条件取个名字,并使用类似 beholder 的事件系统在条件发生时触发事件(以及注册的任何回调函数)。

或(假设你的计时器的 update()love.update() 中被调用),你可以使用你的计时器对象和 every() 方法完成:

local handle = timer:every(0.01, function()
  if condition then
    -- unregister timer, assuming you only want the code to be run once
    timer:cancel(handle)

    -- respond to condition here
  end
end)
2017-02-01 23:20:29