【问题标题】:Function creating in Lua在 Lua 中创建函数
【发布时间】:2013-06-04 15:32:46
【问题描述】:

当我通过分配“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)

【问题讨论】:

    标签: function lua coronasdk


    【解决方案1】:

    这是因为当您执行local countDown = ... 时,countDown 变量不存在,直到 ... 部分已执行。因此,您的函数将访问一个 全局 变量,而不是尚不存在的本地变量。

    请注意,Lua 会将local function countDown ... 转换为以下内容:

    local countDown
    countDown = function ...
    

    【讨论】:

      猜你喜欢
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      • 2019-11-24
      • 1970-01-01
      • 2021-12-28
      相关资源
      最近更新 更多