Loops 错误检测的Lua循环无法正常工作
一两周前,我编写了一个简单的机器人程序,随着我理解的加深,我一直在用新的知识构建它Loops 错误检测的Lua循环无法正常工作,loops,lua,Loops,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 loc
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
这实际上是一个循环,直到我添加了校正检查延迟。如果函数bossCheck()
失败,那么在我看来它应该重复。我是否错误地认为这是可行的,还是我错放了一些循环语句
在我使用local D=1实现这个新代码之前——对于延迟
,我会尝试在我的IOS屏幕上触摸两次,它会返回not true
结果,然后我的循环结束。但到现在为止,我运行我的脚本,但什么都没有发生,而且脚本似乎运行不确定
这很令人困惑。我不希望在这里包含逐字逐句的内容,但这会提示我正确的方向
编辑示例
'函数bossCheck()
如果(getColor(x,y)=“color1”),那么
返回true;
终止
返回false;
结束
函数onBoss()
触摸(x,y)
usleep(时间)
返回true;
结束
函数fightBoss()
触摸屏(x2,y2)
usleep(时间)
返回true;
结束
函数bossReturn()
触摸屏(x3,y3)
usleep(时间)
返回true;
结束
函数bossLoop()
而(博斯克)呢
onBoss();
斗士();
bossReturn();
终止
结束
重复
博斯洛普();
直到(bossCheck==false)
如果(bossCheck==false),则
警报(“Boss循环结束”)
结束
“好,重复,直到
反复执行给定的脚本,直到它到达一个语句
在脚本中,将bossCheck重新定义为一个函数,并检查D是否等于5(D为nil)
你不打任何电话给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
编辑:
我如何按顺序调用多个函数的示例
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
根据您发布的代码,您定义了一个函数bossCheck
,但从未调用过。什么是rgb
?在bossCheck
函数之外的rgb1
是什么?我仍然需要了解一些函数。从我读到的内容来看,函数可以在块中本地设置,是的。Rgb1只是调用函数的字符串getColor
。当我进行了5次左右的颜色检查时,它有助于对它们进行不同的标记。因此,您可以将任何逻辑块设置为函数
?当您调用直到noBoss
时,这意味着检查语句是否已更改为=true
。因此此脚本起作用,if(noBoss)然后提醒我。如果检查返回false
并且实际上有一个boss,我可以添加else
吗?如果有boss,你可以添加else并打印,尽管它永远不会被打印,因为一旦你打破循环,就意味着没有boss是真的,因此它会提醒“没有boss”,打印出有boss,只需在bossCheck函数中添加警报。如果函数返回true
,它将alert
。但是如果它是false
并且返回false
,它将继续循环,直到警报(“无老板”)
。如何在“bossCheck”之后立即添加另一个按顺序调用的函数?为OP添加了一个示例,不知道如何在移动设备上形成代码块。但我的问题是,代码循环是否正确?