Let me try a simple example:
local myCircle = display.newCircle( display.contentCenterX, 0, 50 ) local function deleteCircle() display.remove( myCircle ) myCircle = nil return true end transition.to( myCircle, { time=5000, y = display.contentCenterY, onComplete = deleteCircle() } ) -- vs transition.to( myCircle, { time=5000, y = display.contentCenterY, onComplete = deleteCircle } )
This will create a circle at the top of the content area, move it to the center and in theory remove the circle when it’s done. However, if you look at the first transition.to, because I included the (), that means run the function first and whatever value the function returns (in this case it will return true) assign that to the onComplete listener.
The net result is the circle will be removed immediately since you said “run the function” and you are left with this transition.to statement:
transition.to( myCircle, { time=5000, y = display.contentCenterY, onComplete = true } )
In this case, true is not a function, so onComplete has nothing to do. When you use the second form with out the (), it says assign the value of the function’s memory address (something like 0x9485BA23) to onComplete. Lua knows this is a function and will turn the transition.to into:
transition.to( myCircle, { time=5000, y = display.contentCenterY, onComplete = 0x9485BA23 } )
When the code runs. Now transition.to sees a function assigned to onComplete, it will move the circle and when done, it will call the deleteCircle() function.
So doing:
l
ocal someVar = someFunction
assigns the memory address for someFunction to the variable where as
local someVar = someFunction()
runs someFunction first and takes whatever value is passed back using the “return” statement (or nil as the default).
Rob