When you write:
local function bubba()
Lua creates a variable called “bubba” and stores the address of the function code in that variable so when you write:
bubba()
the program will branch to that function and execute it.
When you see:
local bubba = function()
You are creating an anonymous (unnamed) function and assigning it’s address to the variable “bubba”. In effect you’ve done the exact same thing as above. Two way’s to skin a cat, so to speak.
But where it comes important is a term called “Forward declaration”. In most compiled languages like C, there are “two passes”, the first one builds a large table of all the “symbols” used (i.e variables, function names etc.), then the second pass deals with actually spewing code.
Corona SDK is more of a one pass process and you can’t reference a variable or function before you define it.
Lets look at this scenerio, you want to fade in and out an object:
local function fadeIn(target)
transition.to(target,{time=500, alpha=1.0, onComplete=fadeOut})
end
local function fadeOut(target)
transition.to(target,{time=500, alpha=0.0, onComplete=fadeIn})
end
fadeout()
This will fail in Lua because when the compiler starts looking at the function fadeIn() it tries to reference “fadeOut” which hasn’t been defined yet.
The solution to this problem is to use forward declaration, which means using the second form of the function declaration.
local fadeOut
local function fadeIn(target)
transition.to(target,{time=500, alpha=1.0, onComplete=fadeOut})
end
fadeOut = function(target)
transition.to(target,{time=500, alpha=0.0, onComplete=fadeIn})
end
fadeout()
Now we have declared our intent to use the variable fadeOut early so the compiler knows about it, and later we actually put the function reference into that variable once we know about it. [import]uid: 19626 topic_id: 28230 reply_id: 114072[/import]