在Lua中的'analog of 'arguments.callee''

在 Lua 中有没有一种方法可以引用当前正在执行的匿名函数?就像我们在 JavaScript 中可以使用 arguments.callee 一样。

例如:

local function newLiftAnimator(obj)
  local count = 0
  return function(event)
    -- 在每个 "enterFrame" 事件中在这里动画化 obj 的属性
    obj.y = obj.y - 1
    count = count + 1
    -- 当完成时,删除事件监听器
    if count >= 100 then
      Runtime:removeEventListener("enterFrame", **<this_function>**)
    end
  end
end

Runtime:addEventListener("enterFrame", newLiftAnimator(ball))

原文链接 https://stackoverflow.com/questions/8065851

点赞
stackoverflow用户107090
stackoverflow用户107090

尝试

local f
f = function(event) ... Runtime:removeEventListener("enterFrame", f) ... end
return f
2011-11-09 13:52:26
stackoverflow用户67878
stackoverflow用户67878

不用担心。在阅读了 Lua 邮件列表中的 这个旧消息 后,我想到了一个显而易见的解决方案:

local function newLiftAnimator(obj)
  ...
  local function animator()
    ...
    Runtime:removeEventListener("enterFrame", animator)
  end
  return animator
end
2011-11-09 13:56:11
stackoverflow用户513763
stackoverflow用户513763

另一个可能性是使用:

debug.getinfo(1,'f').func
2011-11-09 13:59:37