Lua Gideros:如何使用多个参数调用函数

3

在我使用Gideros Studio开发的游戏中,我有一个具有多个参数的函数。我想先调用其中一个参数,然后再调用另一个参数。这种做法可行吗?

以下是我的函数:

local function wiggleroom(a,b,c)
    for i = 1,50 do
        if a > b then
            a = a - 1
        elseif a < b then
            a = a + 1
        elseif a == b then
            c = "correct"
        end
    return c
    end
end

我希望将ab进行比较,但稍后在bc上调用该函数。例如:

variable = (wiggleroom(variable, b, c) --if variable was defined earlier
variable2 = (wiggleroom(a, variable2, c)
variable3 = (wiggleroom(a, b, variable3)

我还希望能够将此功能用于多个对象(每个参数调用两次)。


请澄清一下,您在每个函数调用后期望的结果是什么?您是否也希望在函数外部改变a和b的值? - lisu
我不希望函数外改变a和b的值,我只想比较它们。然而,我希望根据这个比较返回c的值。 - Henry V
那么为什么你不能像你说的那样准确地称呼它呢? - lisu
我不知道是否可以为每个参数(x和y坐标)调用两次。 - Henry V
如果您的示例在语法上是正确的,并且使用实际数字a、b、c以及您想要的实际返回值,那么它将更有用。目前来看,不清楚您试图实现什么。 - Oliver
1个回答

2
如果我理解正确的话,您可以考虑使用lua版的类。如果您不了解它们,您可能需要查看这个链接
示例:
tab = {}

function tab:func(a, b, c)  -- c doesn't get used?
    if a then self.a = a end
    if a then self.b = b end
    if a then self.c = c end

    for i = 1,50 do
        if self.a > self.b then
            self.a = self.a - 1
        elseif self.a < self.b then
            self.a = self.a + 1
        elseif self.a == self.b then
            self.c = "correct"
        end
    end
    return c                -- not really necessary anymore but i leave it in
end

function tab:new (a,b,c)    --returns a table
    o = {}
    o.a = a
    o.b = b
    o.c = c
    setmetatable(o, self)
    self.__index = self
    return o
end

                            --how to use:
whatever1 = tab:new(1, 60)  --set a and b
whatever2 = tab:new()       --you also can set c here if needed later in the function

whatever1:func()            --calling your function
whatever2:func(0,64)

print(whatever1.a)          -->51
print(whatever2.a)          -->50
print(whatever1.c)          -->nil
whatever1:func()            --calling your function again
whatever2:func()
print(whatever1.a)          -->60
print(whatever2.a)          -->64
print(whatever1.c)          -->correct
print(whatever2.c)          -->correct

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接