2013-06-08 18 views
6

Atama işlevini oluştururken, "if" koşulu çalışmıyor ancak aşağıdaki ikinci örnekte olduğu gibi işlev oluşturduğumda çalışır. Nedenini söyler misin?Lua'da işlev oluşturma

Çalışmıyor:

local start=os.time() 

local countDown = function(event) 
    if((os.time()-start)==3) then 
     Runtime: removeEventListener("enterFrame", countDown) 
    end 
    print(os.time()-start) 
end 

Runtime:addEventListener("enterFrame", countDown) 

Çalışma:

Eğer local countDown = ..., ne zaman countDown değişken ... parçası çalıştırıldıktan sonra kadar var olmadığından değil
local start=os.time() 

local function countDown(event) 
    if((os.time()-start)==3) then 
     Runtime: removeEventListener("enterFrame", countDown) 
    end 
    print(os.time()-start) 
end 

Runtime:addEventListener("enterFrame", countDown) 

cevap

12

. Böylece, işleviniz henüz mevcut olmayan yerel bir global değişkenine erişecektir.

local countDown 
countDown = function ... 
: Lua aşağıdaki içine local function countDown ... dönüştürür

Not