首页 文章

Lua Loop错误检测无法正常工作

提问于
浏览
0

我在一两个星期前编写了一个简单的机器人程序,并在我开始理解的基础上用新知识构建它 .

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”)然后返回true;结束回报错误;结束

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

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

函数bossReturn()touch(x3,y3)usleep(time)返回true;结束

函数bossLoop()while(bossCheck)做onBoss(); fightBoss(); bossReturn();结束

重复bossLoop();直到(bossCheck == false)

if(bossCheck == false)然后警告(“Boss Loop End”)结束

1 回答

  • 0

    好的, 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 -- if it's true, stop the function here;
        end
        noBoss = true;
        usleep(time * 5); -- no point delaying application action 5 times, when we call just multiply the sleep amount by 5;
    end
    
    repeat
        bossCheck();
    until noBoss;
    
    if (noBoss) then
        alert("No Boss");
    end
    

    Edit:

    我如何按顺序调用多个函数的示例

    function bossCheck()
        if (getColor(x,y) == rgb) -- instead doing 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; -- break out of the loop
        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) -- instead doing 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; -- break out of the loop
        end
    end
    

相关问题