基本Lua问题 - 嵌套在for循环中的if语句

1 lua

我是Lua的业余爱好者,我写了这段代码但是没有编译,我检查了语法的结构,发现它是匹配的,所以我真的不知道出了什么问题,它说18:'结束'预期(在第16行关闭'if')'startFishing'附近,但为什么要这样做????? BTW startFishing是我之前在同一文件中定义的另一个函数.

function detectSuccess()
    local count = 0;
    for x = 448, 1140, 140 do
        color = getColor(x, 170);
        if color == 0xffffff then 
            return false
            startFishing()
        else
            return true
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

mks*_*eve 6

我们正确格式化代码....

function detectSuccess()
   local count = 0;
    for x = 448, 1140, 140 do
        color = getColor(x, 170);
        if color == 0xffffff then 
            return false
            startFishing()
        else
            return true
        end
    end
end

detectSuccess()
Run Code Online (Sandbox Code Playgroud)

startFishing()声明被晃来晃去.语法上唯一可以返回的东西是else或end.

这是lua解析器的抱怨.

来自lua:lua编程4.4

出于语法原因,中断或返回只能作为块的最后一个语句出现(换句话说,作为块中的最后一个语句或者在结束之前,在else之前或者之前).

如果你想startFishing被召唤,它需要在返回之前.例如

function detectSuccess()
   local count = 0;
    for x = 448, 1140, 140 do
        color = getColor(x, 170);
        if color == 0xffffff then 
            startFishing() -- moved before the return
            return false
        else
            return true
        end
    end
end
Run Code Online (Sandbox Code Playgroud)