首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lua Gideros:具有多个参数的调用函数

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

Stack Overflow用户
提问于 2014-10-18 22:37:34
回答 1查看 449关注 0票数 3

在我使用Gideros的游戏中,我有一个具有多个参数的函数。我想在一个参数上调用我的函数,然后在另一个参数上调用函数。这个是可能的吗?

这是我的功能:

代码语言:javascript
复制
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上的函数。例如:

代码语言:javascript
复制
variable = (wiggleroom(variable, b, c) --if variable was defined earlier
variable2 = (wiggleroom(a, variable2, c)
variable3 = (wiggleroom(a, b, variable3)

我还希望能够对多个对象使用这个函数(调用每个参数两次)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-24 06:34:42

如果我正确地理解了你,你可以考虑使用lua版本的类。如果你不认识他们,你可能想看看

示例:

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26445261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档