我的事件侦听器代码如下:
local events = {}
function registerEvent (event)
table.insert(events, event)
end
function unregisterEvent (event)
table.remove(events, event)
end
function processEvents ()
for _, event in pairs(events) do
for _, condition in pairs(event.conditions) do
if condition() then
event.triggered = true;
else
event.triggered = false;
break
end
end
if event.triggered then
event.callback()
end
end
end
registerEvent
{
conditions = {
function () return Keyboard.Shift == true end
},
callback = function () text = "triggered" end
}
registerEvent
{
conditions = {
function () return Keyboard.Shift == false end
},
callback = function () text = "not triggered" end
}
更新循环代码简单:
function OnUpdate(deltaTime)
processEvents()
end
基本上我想要的是,在条件是正确的情况下,只有一次触发一次,而不是每次其他更新(然后在条件不正确时触发检查重置)。
<</p>根据评论中的讨论,您可以按照以下修改对processEvents
进行以下修改:
function processEvents ()
for _, event in pairs(events) do
for _, condition in pairs(event.conditions) do
if condition() then
event.triggered = true;
else
event.triggered = false;
event.triggeredbefore = false
break
end
end
if event.triggered and not event.triggeredbefore then
event.callback()
event.triggeredbefore = true
end
end
end