Аналог 'arguments.callee' в Lua - PullRequest
       21

Аналог 'arguments.callee' в Lua

2 голосов
/ 09 ноября 2011

Есть ли способ сослаться на выполняющуюся в настоящее время анонимную функцию в Lua? Так же, как мы можем сделать в JavaScript с 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 ]

4 голосов
/ 09 ноября 2011

Попробуйте

local f
f=function (event) ... Runtime:removeEventListener("enterFrame", f) ... end
return f
2 голосов
/ 09 ноября 2011

Неважно. Прочитав это старое сообщение в списке рассылки Lua, я нашел очевидное решение:

local function newLiftAnimator(obj)
  ...
  local function animator()
    ...
    Runtime:removeEventListener("enterFrame", animator)
  end
  return animator
end
1 голос
/ 09 ноября 2011

Другая возможность использует:

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