2014-10-18 5 views
1

Gideros Studio를 사용하는 게임에서 여러 매개 변수가있는 함수가 있습니다. 하나의 매개 변수에서 내 함수를 호출하고 나중에 다른 함수에서 호출하려고합니다. 이것이 가능한가?루아 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 

내가 b와 비교 될 a을 원하지만 나중에 bc에 함수를 호출 :

여기 내 기능입니다. 예를 들면 다음과 같습니다.

이 기능을 여러 개체에 사용할 수도 있습니다 (각 매개 변수를 두 번 호출 할 수 있음).

+0

명확히하십시오, 각 함수 호출 후 예상되는 결과가 무엇인지. 함수 밖에서 a와 b의 값을 변경하고 싶습니까? – lisu

+0

a와 b가 함수 외부에서 변경되는 것을 원하지 않습니다. 단지 비교할 수 있기를 바랍니다. 그러나이 비교를 기반으로 c 값이 반환되기를 바랍니다. –

+0

그런데 왜 당신이 진술 한대로 정확하게 부를 수 없습니까? – lisu

답변

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 
관련 문제