transition.to don't work correctly

Hi, I’m doing a project where the player at the end of a move executes a function,

This is my example code:

local player = display.newRect(160, 400, 25, 25) player:setFillColor(0,0,1) local function onc() print ("I'm done????, onComplete dont work :-(, I'm moving still") end transition.to(player, { x = 160, y = 0 ,time = 5000, onComplete = onc()})

When execute transition.to, the function onComplete runs without finishing the transition.

This is a bug? Any can test the code?

Regards.

Hi @ramir1010,

For the value of the “onComplete”, don’t specify the parentheses following. You’re not actually “calling” that function there, but instead the transition library is, and it doesn’t like the parentheses there (it won’t make the call basically).

This is the proper format:

[lua]

transition.to( player, { x = 160, y = 0 , time = 5000, onComplete = onc } )

[/lua]

Take care,

Brent

Thank you for you response Brent, but i want pass variables to the function onc,

transition.to( player, { x = 160, y = 0 , time = 5000, onComplete = onc(var1,var2,var3) } )

it’s possible? There is another way?

Regards.

transition.to( player, { x = 160, y = 0 , time = 5000, onComplete = function() onc(var1,var2,var3) end } )

Thanks!!!

Yes, that’s a totally valid way (an anonymous function like @roaminggamer suggests). However, sometimes it’s not practical to write a separate embedded function for every transition in your game… you may need to call a common “onComplete” function from many transitions, passing different variables to it each time.

In that case, you could set a temporary table of variables as a property on the object itself, then retrieve those variables in the “onComplete” listener function:

[lua]

local function onc( obj )

    local vars = obj.vars

    print( vars[1], vars[2], vars[3] )

    obj.vars = nil  – Clear these variables

end

local player = display.newRect( 0,0,10,10 )

– Set a temporary table of variables as property of player object

player.vars = { “a”, “b”, 3 }

– Transition the player

transition.to( player, { x = 160, y = 160 , time = 5000, onComplete = onc } )

[/lua]

As you can see, the “onComplete” listener function receives a reference to the object that was transitioned (player) as its sole argument, so you can grab the table of variables from that and use them as needed. Then you can clear those variables (nil) so that they don’t get mixed up in some other way later.

Hope this helps,

Brent

Hi @ramir1010,

For the value of the “onComplete”, don’t specify the parentheses following. You’re not actually “calling” that function there, but instead the transition library is, and it doesn’t like the parentheses there (it won’t make the call basically).

This is the proper format:

[lua]

transition.to( player, { x = 160, y = 0 , time = 5000, onComplete = onc } )

[/lua]

Take care,

Brent

Thank you for you response Brent, but i want pass variables to the function onc,

transition.to( player, { x = 160, y = 0 , time = 5000, onComplete = onc(var1,var2,var3) } )

it’s possible? There is another way?

Regards.

transition.to( player, { x = 160, y = 0 , time = 5000, onComplete = function() onc(var1,var2,var3) end } )

Thanks!!!

Yes, that’s a totally valid way (an anonymous function like @roaminggamer suggests). However, sometimes it’s not practical to write a separate embedded function for every transition in your game… you may need to call a common “onComplete” function from many transitions, passing different variables to it each time.

In that case, you could set a temporary table of variables as a property on the object itself, then retrieve those variables in the “onComplete” listener function:

[lua]

local function onc( obj )

    local vars = obj.vars

    print( vars[1], vars[2], vars[3] )

    obj.vars = nil  – Clear these variables

end

local player = display.newRect( 0,0,10,10 )

– Set a temporary table of variables as property of player object

player.vars = { “a”, “b”, 3 }

– Transition the player

transition.to( player, { x = 160, y = 160 , time = 5000, onComplete = onc } )

[/lua]

As you can see, the “onComplete” listener function receives a reference to the object that was transitioned (player) as its sole argument, so you can grab the table of variables from that and use them as needed. Then you can clear those variables (nil) so that they don’t get mixed up in some other way later.

Hope this helps,

Brent