0

有没有办法缩短这段代码?

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
...
next all the same with sleep values only changing

我想使用重复直到,但我不能这样做,因为睡眠的值正在改变。有没有办法将睡眠值保存在表中(即 50、200、100、75、25、200),这样我就可以在代码中使用 Repeat-Until?我一直在尝试搜索,但我是 Lua 的新手。任何帮助表示赞赏,谢谢

4

1 回答 1

1

由于您想循环一系列超时,因此我不会使用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" -- You should somehow indicate timeout to the caller
end
于 2018-05-18T23:56:35.100 回答