Fade in objects one by one

local function textFadeIn(group,i) if group[i] ~= nil then transition.fadeIn( group[i], { time = 1000, onComplete = textFadeIn(group,i+1)} ) end end textFadeIn(textGroup,1)

Hi all, I am new to Corona SDK. I want to fade in all text object in a display group one by one. The group[i+1] should start the transition after the group[i] complete the transition. However, in the above code, all the object will transition at the same time.

Is there any problem in the code? Thanks.

Wing,

add a delay field to the fadeIn function …  multiply the delay time by i, then each item will fade in 

local function textFadeIn(group, i) if group[i] ~= nil then transition.fadeIn( group[i], {delay = i \* 4000, time = 1000, onComplete = textFadeIn(group, i+1)} ) end end

 as in nearly all cases, there are multiple ways to do things…  this way works, but there are likely more efficient wats to do the same thing.

If you want the first image to start fade in immediately, just change the delay argument to this  :  delay = (i-1) * 4000

Best of luck in your coding.

Bob

The problem here is the listener you specify for onComplete.

If you write

onComplete = function() textFadeIn(group,i+1) end

instead of

onComplete = textFadeIn(group,i+1)

it works. Rob posted a very good explanation here:

https://forums.coronalabs.com/index.php?app=forums&module=forums&section=findpost&pid=389155

Hope that helps :slight_smile:

Wing, 

Chris is correct here.  An anonymous function is probably the best way to do what you are wanting to do there.  Mine was just a quick ‘hack’ fix, which works, but it’s bad because it ignores the issue that Chris was smart enough to point out. 

It is best to do it the right way.  But more importantly, it would be most helpful if you do  take the time to read Rob’s explanation link Chris provided.  Rob is excellent at explaining these type things.  Once you get a grasp of how it works and why; it will help save you a lot of headaches later on. 

Good luck!

Bob

Chris & Bob,

It works. Thanks for answering. I love you all so much.

Wing

Wing,

add a delay field to the fadeIn function …  multiply the delay time by i, then each item will fade in 

local function textFadeIn(group, i) if group[i] ~= nil then transition.fadeIn( group[i], {delay = i \* 4000, time = 1000, onComplete = textFadeIn(group, i+1)} ) end end

 as in nearly all cases, there are multiple ways to do things…  this way works, but there are likely more efficient wats to do the same thing.

If you want the first image to start fade in immediately, just change the delay argument to this  :  delay = (i-1) * 4000

Best of luck in your coding.

Bob

The problem here is the listener you specify for onComplete.

If you write

onComplete = function() textFadeIn(group,i+1) end

instead of

onComplete = textFadeIn(group,i+1)

it works. Rob posted a very good explanation here:

https://forums.coronalabs.com/index.php?app=forums&module=forums&section=findpost&pid=389155

Hope that helps :slight_smile:

Wing, 

Chris is correct here.  An anonymous function is probably the best way to do what you are wanting to do there.  Mine was just a quick ‘hack’ fix, which works, but it’s bad because it ignores the issue that Chris was smart enough to point out. 

It is best to do it the right way.  But more importantly, it would be most helpful if you do  take the time to read Rob’s explanation link Chris provided.  Rob is excellent at explaining these type things.  Once you get a grasp of how it works and why; it will help save you a lot of headaches later on. 

Good luck!

Bob

Chris & Bob,

It works. Thanks for answering. I love you all so much.

Wing