onComplete bug in transition.to

I suspect this bug may extend to other functions that allow listeners passed in.

Here’s a line of code that I had in my game…

transition.to( player.sprite, { x = nx, y = ny, time = 2000, onComplete = change\_room\_done( nx, ny) } )

You’ll notice that i am passing parameters into the onComplete listener parameter. The documentation does not say that you cannot pass parameters.

Well, when this line executes, it calls the onComplete function immediately and with the parameters passed!

Deleting the parameters like this…

transition.to( player.sprite, { x = nx, y = ny, time = 2000, onComplete = change\_room\_done } )

…makes it work as expected.

I don’t know why this happens, but there’s no error from Corona when it encounters parameters passed into a function - it just calls it immediately, which screws up the entire callback chain. Tough to debug.

RULE: Don’t put parameters after listener function names!

Ok, I figured out why this happened. This is a Lua feature, so it’s my fault.

onComplete is being assigned the return value from change_room_done( nx, ny )!!

That is why the function is called first: to get the return value (nil) and assign it to onComplete… which means the function will never get called after the transition ends.

My bad.

Again: Don’t put parameters after listener function names – you are actually calling the function at that point and the return value is being assigned as the listener function!

You could still achieve this behavior if you use a closure.

Ex:

transition.to( mySprite, { x = 100, onComplete = function( event ) yourFunc( x, y ) end } )

Hope this helps

Great advice, thank you!

You are most welcome.
Thank you for Wolfenstein 3D, one of my favorite games of all time.

I was too scared to play it as a kid :wink:
Cheers

Ok, I figured out why this happened. This is a Lua feature, so it’s my fault.

onComplete is being assigned the return value from change_room_done( nx, ny )!!

That is why the function is called first: to get the return value (nil) and assign it to onComplete… which means the function will never get called after the transition ends.

My bad.

Again: Don’t put parameters after listener function names – you are actually calling the function at that point and the return value is being assigned as the listener function!

You could still achieve this behavior if you use a closure.

Ex:

transition.to( mySprite, { x = 100, onComplete = function( event ) yourFunc( x, y ) end } )

Hope this helps

Great advice, thank you!

You are most welcome.
Thank you for Wolfenstein 3D, one of my favorite games of all time.

I was too scared to play it as a kid :wink:
Cheers