Function lua中的正向函数参数arg

Function lua中的正向函数参数arg,function,parameters,lua,arguments,Function,Parameters,Lua,Arguments,我想重写一个函数来检查它的参数值,但函数调用它并正常传递原始参数。这可能吗?我正在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 = f

我想重写一个函数来检查它的参数值,但函数调用它并正常传递原始参数。这可能吗?我正在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" )

在新的
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()!如果
newCircle
接受两个参数,为什么要将
的问题复杂化…
?您应该在问题中保留原始(有问题的)代码。如果你用正确的版本替换它,其他人很难看到问题的原意。