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)
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With