Reputation: 12579
When I create the function by assigning ,"if" condition doesn't work but when I do create the function like in second example below, it works. Can you tell me why?
Not working:
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)
Working:
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)
Upvotes: 6
Views: 247
Reputation: 474436
That's because when you do local countDown = ...
, the countDown
variable doesn't exist until after the ...
part has been executed. So your function will access a global variable, not the local one that doesn't exist yet.
Note that Lua converts local function countDown ...
into the following:
local countDown
countDown = function ...
Upvotes: 12