Is this a bug with trasition.to() onComplete?

So in my code I added a transition with an onComplete if I set it as  { onComplete=myFunction }

it does not trigger the first time on the second time the transition is called it runs myFunction

So this fails to execute showAll(event) when I see the transition complete so I know it at least runs the width/height portion of the transition. if I run it more than once it runs the onComplete each subsequent run

 local bg = display.newRect(myGroup, 0, 0, 50, 50) bg:setFillColor(1,1,1,1) transition.to( bg, { time=200, width=500, height=500, onComplete=showAll } ) function showAll(event) local rect = display.newRect(myGroup, 5, \_H - 185, \_W - 10, 180) transition.to( viewGroup, { time=200, alpha=1 } ) end

This on the other hand works on all calls to that transition, the only difference is that I wrapped the event function with a function() … end

 local bg = display.newRect(myGroup, 0, 0, 50, 50) bg:setFillColor(1,1,1,1) transition.to( bg, { time=200, width=500, height=500, onComplete=function(event) showAll(event) end } ) function showAll(event) local rect = display.newRect(myGroup, 5, \_H - 185, \_W - 10, 180) transition.to( viewGroup, { time=200, alpha=1 } ) end

I am going to use the latter but not sure if there is a reason why it occurs.

Tim,

I think its actually a question of scope and visibility.  

In your original code, ‘showAll’ has not been defined yet, when you try to reference it in the transition call.

(i.e. It’s like assigning nil to onComplete in that table.)

In your second example, it doesn’t get called till later and has thus been defined by the time the all occurs.

Warning: In either case, you’re making showAll a global function which is a bad practice and will bite you eventually.

I know, I know… so how do you fix this problem?  Well, that’s hard to say since I see things in your code I don’t have context for, however, here is a partial fix:

 local bg = display.newRect(myGroup, 0, 0, 50, 50) bg:setFillColor(1,1,1,1) bg.onComplete = function( self, event ) -- For safety I added this: if( self.removeSelf == nil) then return end -- This will happen if 'bg' got removed. -- self.parent in this case will be myGroup, but it's better than a direct -- reference to myGroup as it makes the code more portable local rect = display.newRect( self.parent , 5, \_H - 185, \_W - 10, 180) transition.to( viewGroup, { time=200, alpha=1 } ) -- I had to leave the reference to 'viewGroup' alone since I didn't know what it was -- or its hierarchy. However, this makes the code un-portable. end -- Now by specifying a field on bg called 'onComplete', we can simply pass bg -- as the onComplete reference. -- -- Corona will automatically detect the 'attached' function and use it. -- transition.to( bg, { time=200, width=500, height=500, onComplete=bg } )

The above is a coding trick by the way.  You see it all the time in Corona for all kinds of listeners, etc.

Thanks, I had not seen that little trick before, but very useful at times :slight_smile:

I appreciate the answer thank you.

Tim,

I think its actually a question of scope and visibility.  

In your original code, ‘showAll’ has not been defined yet, when you try to reference it in the transition call.

(i.e. It’s like assigning nil to onComplete in that table.)

In your second example, it doesn’t get called till later and has thus been defined by the time the all occurs.

Warning: In either case, you’re making showAll a global function which is a bad practice and will bite you eventually.

I know, I know… so how do you fix this problem?  Well, that’s hard to say since I see things in your code I don’t have context for, however, here is a partial fix:

 local bg = display.newRect(myGroup, 0, 0, 50, 50) bg:setFillColor(1,1,1,1) bg.onComplete = function( self, event ) -- For safety I added this: if( self.removeSelf == nil) then return end -- This will happen if 'bg' got removed. -- self.parent in this case will be myGroup, but it's better than a direct -- reference to myGroup as it makes the code more portable local rect = display.newRect( self.parent , 5, \_H - 185, \_W - 10, 180) transition.to( viewGroup, { time=200, alpha=1 } ) -- I had to leave the reference to 'viewGroup' alone since I didn't know what it was -- or its hierarchy. However, this makes the code un-portable. end -- Now by specifying a field on bg called 'onComplete', we can simply pass bg -- as the onComplete reference. -- -- Corona will automatically detect the 'attached' function and use it. -- transition.to( bg, { time=200, width=500, height=500, onComplete=bg } )

The above is a coding trick by the way.  You see it all the time in Corona for all kinds of listeners, etc.

Thanks, I had not seen that little trick before, but very useful at times :slight_smile:

I appreciate the answer thank you.