2011-11-09 28 views
1

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

例如为:

local function newLiftAnimator(obj) 
    local count = 0 
    return function(event) 
    -- animate obj's properties here on each "enterFrame" event 
    obj.y = obj.y - 1 
    count = count + 1 
    -- when done, remove event listener 
    if count >= 100 then 
     Runtime:removeEventListener("enterFrame", **<this_function>**) 
    end 
    end 
end 

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

回答

3

尝试

local f 
f=function (event) ... Runtime:removeEventListener("enterFrame", f) ... end 
return f 
1

没关系。在Lua的邮件列表阅读this old message后,我想出了一个显而易见的解决方案:

local function newLiftAnimator(obj) 
    ... 
    local function animator() 
    ... 
    Runtime:removeEventListener("enterFrame", animator) 
    end 
    return animator 
end 
0

另一种可能性是使用:

debug.getinfo(1,'f').func