List Iteration - Increasing Opacity

I am spinning my wheels on this.

I have a grid of cells to load each displaying a seperate sprite on a sheet.
However, I want to load them in gradually. My cells are loaded as individual objects in a LIST.

So on the first update the first cell will start at 20% opacity.
On the second update the first cell will be at 40% and the second cell will be at 20%.
On the third update the first cell be at 60%, second cell at 40% and third cell at 20%.
This pattern would continue until every cell is at 100%

I have figured out the sprite sheet part, the opacity, drawing the texture, etc, etc. Its just the loop I can’t figure out.

Either I end up with everything loading at once. Only one cell loading. A mixture of cells. A mixture of opacity values across the cells. I even got them all phasing in correctly as one big group but I haven’t been able to figure out how to get them to gradually load in.

Below is the code I wrote that doesn’t really do work. The idea would be to go through the list and keep a “buffer” value of the previous cell. Then based on that value determine the value of the current cell. One problem I already see is that I am going to have issues with setting the first or last cell properly because the buffer will either not be present or the last cell will never reach the appropriate value.

Blockquote
int jpsMenuObjectPhaseBuffer = 0;
if (timeSinceLastMenuLoad > .075f)
{
foreach (JPSMenuGraphic x in demo.jpsMenuObjects)
{
if (jpsMenuObjectPhaseBuffer == 1)
{
x.phase = 1;
x.opacity = .20f;
}
else if (jpsMenuObjectPhaseBuffer == 2)
{
x.phase = 2;
x.opacity = .40f;
}
else if (jpsMenuObjectPhaseBuffer == 3)
{
x.phase = 2;
x.opacity = .80f;
}
else if (jpsMenuObjectPhaseBuffer == 3)
{
x.phase = 3;
x.opacity = 1.0f;
}
else
{
x.phase = 1;
x.opacity = .20f;
}

	jpsMenuObjectPhaseBuffer = x.phase;
}
timeSinceLastMenuLoad = 0f;

}

Alright, I don’t know if this is a good solution but it works:

Basically, instead of looking at the previous object and trying to account for that value and getting all tangled up with a foreach. I switched to a for loop and every time the for loop finished completely, I would increase a counter. Then on the next game update the loop would go through and since the counter increased it would go to the next cell automatically and then the if statements would check what phases the cell was on to adjust the opacity.

            for (int i = 0; i < demo.jpsMenuObjects.Count; i++)
            {
                if(i <= jpsMenuObjectsPasses)
                {
                    if(demo.jpsMenuObjects[i].phase == 0)
                    {
                        demo.jpsMenuObjects[i].phase = 1;
                        demo.jpsMenuObjects[i].opacity = 0.3f;
                    }
                    else if (demo.jpsMenuObjects[i].phase == 1)
                    {
                        demo.jpsMenuObjects[i].phase = 2;
                        demo.jpsMenuObjects[i].opacity = 0.67f;
                    }
                    else  if (demo.jpsMenuObjects[i].phase == 2)
                    {
                        demo.jpsMenuObjects[i].phase = 3;
                        demo.jpsMenuObjects[i].opacity = 1.0f;
                    }
                }
            }
            jpsMenuObjectsPasses++;

What about give each cell object an opacityID, starting from 0 - in order of what you would like there opacity to start appearing.

Have a globalOpacityID on your main loop, starting from 0. Increment this by 1 every loop

Pass the globalOpacityID in through your cell update.

On each cell update if the opacity on the cell is not 100% yet, then check if the globalOpacityID is higher than the cells OpacityID. If it is, then increaese that cells opacity by 20%

This way you can also have cells have the same opacityID if you wanted 2 cells to fade in at the same time. You also wont have to rely on ordering them in the correct order in the list.