2014-10-18 27 views
1

在我使用Gideros Studio的遊戲中,我有一個具有多個參數的功能。我想在一個參數上調用我的函數,然後在另一個參數上調用我的函數。這可能嗎?Lua Gideros:具有多個參數的呼叫功能

這裏是我的功能:

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) 

我也希望能夠將這個函數用於多個對象(每個參數調用兩次)。

+0

請澄清,什麼是你在每個函數調用後預期的結果。你想要在功能之外更改a和b的值嗎? – lisu 2014-10-18 23:57:41

+0

我不想讓a和b在函數之外被更改,我只是希望將它們進行比較。但是,我想根據這個比較來返回c的值。 – 2014-10-19 00:53:24

+0

那你爲什麼不能像你說的那樣完全調用它? – lisu 2014-10-19 01:01:07

回答

1

如果我正確理解你,你可以考慮使用lua版本的類。如果你不知道他們,你可能想看看 this

例如:

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