Lua 循环中的变量检查

提前道歉,因为我确定这个问题已经被回答过了,但是由于我没有任何编程经验,很难将其他帖子中的解决方案翻译到我的代码中。我有一个 for 循环,我想在每次间隔期间检查全局变量。下面的代码不起作用,因为它认为"continue_loop"是一个本地变量。有什么建议吗?

if (event == "MOUSE_BUTTON_PRESSED" and arg == 1) then
continue_loop = 1
Click()
end

if (event == "M_RELEASED" and arg == 3) then
Click()
end

if (event == "MOUSE_BUTTON_RELEASED" and arg == 1) then
Stopclick()
end

function Stopclick()
continue_loop = 0
end

function Click()
    PressMouseButton(1)
    Sleep (10)
    ReleaseMouseButton(1)
  for i=1,10 do
    if (continue_loop == 1) then
        MoveMouseRelative(0,5)
        Sleep (30)
    else return
    end
    end
  if (continue_loop == 0) then
    Stopclick()
  elseif (continue_loop == 1) then SetMKeyState(3)
  else Stopclick()
  end
end
点赞
用户9185797
用户9185797

你可以在程序开头简单地写一个 local continue_loop。 我建议你学习一下变量的可见性范围和生命周期,以更好地理解这个解决方案!

2018-12-19 20:05:38
用户7396148
用户7396148

Lua 只有一个单一线程,这意味着在你的 for 循环期间,continue_loop 变量将不会改变,因为循环内没有任何代码试图改变它。

你需要调用一个函数检查鼠标状态,然后更新 continue_loop 变量。

for i=1,10 do
    CheckMouseState() -- 基于鼠标状态设置 continue_loop 的全局值。
    if (continue_loop == 1) then
        MoveMouseRelative(0,5)
        Sleep (30)
    else return
    end
end
2018-12-19 21:58:41