【问题标题】:Forward function parameter arg in lualua中的转发函数参数arg
【发布时间】:2012-11-08 09:37:29
【问题描述】:

我想重写一个函数来检查它的参数值,但调用它并正常传递原始参数。这可能吗?我正在使用 www.coronalabs.com 的 Corona SDK

我目前无法使用的代码是:

-- getting a refrence to the original function so i can replace it with my overriding function
local newcircle = display.newCircle

-- my override
display.newCircle = function(...)
-- attempt to pass the parameters to this function on to the real function
local t = {...}
newcircle(unpack(t))
end

-- calling the overridden function as if it were normal
display.newCircle( display.newGroup(), "image.png" )

【问题讨论】:

  • 如果newCircle 接受两个参数,为什么要用... 使事情复杂化?
  • 您应该将原始(有问题的)代码留在问题中。如果换成修正版,其他人很难看清问题的原意。

标签: function parameters lua arguments


【解决方案1】:

在新的display.newCircle 实现中,您使用的是未定义的t 表和已弃用的arg 自动表。

试试这个:

-- my override
display.newCircle = function(...)
    local t = {...} -- you need to collect arguments to a table
    -- dumb use of override
    print(t[1])
    -- attempt to pass the parameters to this function on to the real function
    newcircle(unpack(t))
end

【讨论】:

  • 感谢您指出这一点-您是对的,我当时特别笨。但是我得到的错误是:'newcircle'的错误参数#2(预期数字,得到字符串)
  • 我相信问题的发生是因为表的解包返回了表条目的索引值以及条目。我只是没有办法绕过它。
  • unpack 只返回值,不返回键。在您的代码中,您有效地传递了一个字符串作为第二个参数:"image.png"
  • 谢谢 :) - 我传递的参数不正确 - 我应该使用调用来覆盖 newImage() !
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 2018-07-12
  • 2020-01-02
  • 1970-01-01
  • 2017-06-05
相关资源
最近更新 更多