I thought local variables and function where only accessible in the block that they are declared. So why does something like this work?
[code]
local disappear = function ()
mo.alpha = 0
end
local appear = function ()
–Take .3 seconds to appear
transition.to(mo, {time = 300,alpha = 1})
–Disappear after .5 seconds
timer.performWithDelay(moDisappearTime, disappear, 1)
end
There is a slight difference between the two function defines.
local x1 = function()
print( "local disappear: " .. tostring(x1) ) -- prints "nil"
end
local function x2()
print( "local disappear: " .. tostring(x2) ) -- prints the function address of x2
end
x1()
x2()
If your function is going to call itself (recursion) you need to use the second form (x2) so the function name is defined first. In the first form, x1 isn’t define until the end of the function.
Printing out a variable, object, or function using “tostring” is a good debug tool to verify that it’s within the scope of your code. If it prints 'nil" that generally means it hasn’t been defined or not accessible.