Lua循环与错误检测未能正确工作。

我几周前编写了一个简单的机器人程序,并在我不断扩大理解的新知识基础上不断完善它。

local B = 1 --Boss check 1 = true, 2 = false

repeat
  function bossCheck()
    local rgb1 = getColor(x,y)
    if rgb1 == (rgb) then
      touchDown(x,y)
      usleep(time)
      touchUp(x,y)
    end

    local D = 1 --Delay, corrective action when script is out of sync with loading times
    repeat
      if rgb1 ~= (rgb) then
        D = D + 1
        usleep(time)
      end
    until D == 5
  end
  if D == 5 then
    B = B + 1
  end
until B == 2

if B == 2 then
  alert("No Boss")
end

这实际上可以循环执行,直到我添加了纠正检查延迟。如果 function bossCheck() 失败,那么在我的想法中它应该 repeat。我以为这个是可行的,或者我错把代码块的位置了?

在我加入 local D = 1 --for delay 的代码之前,我会尝试在我的 iOS 屏幕上触摸两次,它会返回 not true 的结果,然后我的循环就结束了。但是现在,我运行我的脚本,什么都不会发生,而且似乎脚本无限运行。

这非常令人困惑。我不指望在这里得到逐字逐句的代码,但是请指引我朝正确的方向努力。

编辑 - 示例

function bossCheck ()
if (getColor(x,y) == "color1") then
return true;
end
return false;
end

function onBoss ()
touch(x,y)
usleep(time)
return true;
end

function fightBoss ()
touch(x2,y2)
usleep(time)
return true;
end

function bossReturn ()
touch (x3,y3)
usleep(time)
return true;
end

function bossLoop ()
while (bossCheck) do
onBoss ();
fightBoss ();
bossReturn ();
end
end

repeat
bossLoop ();
until (bossCheck == false)

if (bossCheck == false) then
alert("Boss Loop End")
end
点赞
用户4261756
用户4261756

好的,repeat until会执行给定的脚本,直到执行到某个语句。

你的脚本重新定义了bossCheck函数,并检查D是否等于5(D为空)。

你没有在任何地方调用bossCheck,所以B仍然为1。

这个脚本应该工作。

local noBoss = false;
function bossCheck()
    noBoss = false;
    local rgb1 = getColor(x,y);
    if (rgb1 == rgb) then
      touchDown(x,y)
      usleep(time)
      touchUp(x,y)
      return -- 如果为真,停止该函数的执行;
    end
    noBoss = true;
    usleep(time * 5); -- 调用时没有意义将应用程序操作延迟5倍,当我们调用只需将睡眠量乘以5;
end

repeat
    bossCheck();
until noBoss;

if (noBoss) then
    alert("No Boss");
end

编辑:

我将演示如何按顺序调用几个函数的示例

function bossCheck()
    if (getColor(x,y) == rgb) -- 而不是 rgb1=..
        touchDown(x,y)
        usleep(time)
        touchUp(x,y)
        return true;
    end
    return false;
end
while true do
    if (not bossCheck()) then
        alert("No Boss");
        break; -- 退出循环
    end
    if ((function () return true)()) then
        alert("This local function return true.. so alert message is shown\nYou can do the same with any other functions");
    end
end

根据你的示例,你可以这样做

function bossCheck()
    if (getColor(x,y) == rgb) -- 而不是 rgb1=..
        return true;
    end
    return false;
end

while true do
    if (bossCheck()) then
        touch(x,y)
        usleep(time)
        touch(x2,y2)
        usleep(time)
        touch(x3,y3)
        usleep(time)
    else
        alert("No Boss");
        break; -- 退出循环
    end
end
2015-02-05 13:10:32