Function Lua中的函数创建

Function Lua中的函数创建,function,lua,coronasdk,Function,Lua,Coronasdk,当我通过赋值来创建函数时,“if”条件不起作用,但当我像下面第二个示例那样创建函数时,它起作用。你能告诉我为什么吗 不工作: local start=os.time() local countDown = function(event) if((os.time()-start)==3) then Runtime: removeEventListener("enterFrame", countDown) end print(os.time()-start) end

当我通过赋值来创建函数时,“if”条件不起作用,但当我像下面第二个示例那样创建函数时,它起作用。你能告诉我为什么吗

不工作:

local start=os.time()

local countDown = function(event)
   if((os.time()-start)==3) then
      Runtime: removeEventListener("enterFrame", countDown)
   end
   print(os.time()-start)
end

Runtime:addEventListener("enterFrame", countDown)
工作:

local start=os.time()

local function countDown(event)
   if((os.time()-start)==3) then
      Runtime: removeEventListener("enterFrame", countDown)
   end
   print(os.time()-start)
end

Runtime:addEventListener("enterFrame", countDown)

这是因为当您执行
本地倒计时=…
时,
倒计时
变量在执行
部分之后才存在。因此,您的函数将访问一个全局变量,而不是尚不存在的局部变量

请注意,Lua将
本地函数倒计时…
转换为以下内容:

local countDown
countDown = function ...