如何缩短Lua代码

有没有办法缩短以下代码?

if (event == "MOUSE_BUTTON_PRESSED" and arg == 1) then
PressKey ("a")
Sleep (50)
if not IsMouseButtonPressed(1) then
ReleaseKey ("a")
return
end
PressKey ("a")
Sleep (200)
if not IsMouseButtonPressed(1) then
ReleaseKey ("a")
return
end
...
接下来都一样,只有睡眠值不同

我想使用repeat-until,但是由于睡眠时间的值在变化,我无法这样做。有没有办法将睡眠值保存在表中(例如50、200、100、75、25、200),以便我可以在代码中使用Repeat-Until?我一直在尝试搜索,但我对Lua还很陌生。感谢任何帮助。

点赞
用户1944004
用户1944004

由于您想循环超时范围,我建议不要使用 repeat until,而应该使用一个 for 循环。最重要的是,在完成列表之后,您应该通知调用者所有的超时都失败了。

if (event == "MOUSE_BUTTON_PRESSED" and arg == 1) then
    for _,duration in ipairs{50, 200, 100, 75, 25, 200} do
        PressKey ("a")
        Sleep (duration)
        if not IsMouseButtonPressed(1) then
            ReleaseKey ("a")
            return
        end
    end
    return "ERROR"-- 您应该以某种方式向调用者指示超时
end
2018-05-18 23:56:35