使用 Lua Gideros:调用具有多个参数的函数。

在我使用 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) --如果变量在之前已定义
variable2 = (wiggleroom(a, variable2, c)
variable3 = (wiggleroom(a, b, variable3)

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

点赞
用户4173441
用户4173441

如果我理解正确,您可以考虑使用类的lua版本。如果您不知道它们,您可能想看看this

示例:

tab = {}

function tab:func(a, b, c)  -- c不被使用?
    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                -- 不是必要的了,但我保留它
end

function tab:new(a, b, c)  --返回一个表
    o = {}
    o.a = a
    o.b = b
    o.c = c
    setmetatable(o, self)
    self.__index = self
    return o
end

--如何使用:
whatever1 = tab:new(1, 60)  --设置a和b
whatever2 = tab:new()       --您也可以在函数中稍后设置c(如果需要)

whatever1:func()            --调用您的函数
whatever2:func(0, 64)

print(whatever1.a)          -->51
print(whatever2.a)          -->50
print(whatever1.c)          -->nil
whatever1:func()            --再次调用您的函数
whatever2:func()
print(whatever1.a)          -->60
print(whatever2.a)          -->64
print(whatever1.c)          -->correct
print(whatever2.c)          -->correct
2014-10-24 06:34:42