Pleerock
Pleerock

Reputation: 33

How to transfer parameters to the function in onComplete event (Lua + Corona)

How to transfer parameters to the function in onComplete event (Lua + Corona SDK)

transition.to(obj, {time = 1000, x = toEnemy.x, y = toEnemy.y, onComplete = onHit} )

----
transition.to( target, params )
----

params is:

params.time
params.transition
params.delay
params.delta
params.onStart
params.onComplete

there isn't "params.onCompleteParams", but I want to transfer parameters to my

function without using global variables

Upvotes: 1

Views: 5377

Answers (1)

Michal Kottman
Michal Kottman

Reputation: 16753

Lua functions are actually closures. This means that they capture the values of local variables outside the function that are in their scope.

By using an anonymous function as the onComplete handler, you can do the following:

-- save 'parameters' you need to pass as local variables
local paramToPass = 'hello'
local paramToPass2 = 'world'
transition.to(obj, { time = 1000, x = toEnemy.x, y = toEnemy.y,
    -- use an anonymous function as the onComplete handler
    -- it captures the values of any local variables it references
    onComplete = function(obj)
        -- call your original function with your additional parameters...
        onHit(obj, paramToPass, paramToPass2) 
    end
})

Upvotes: 9

Related Questions