Question

I am a beginner with XNA Game Studios and want to learn some basics in game programming. I am currently creating a small space shooter, something like a variation of Space Invaders.

To make my rockets look better I've created some smoke trails to follow them, but would like to remove them after some time (400 milliseconds) so the screen is not blocked by that smoke. To achieve this I have created the following code, which seems pretty logical to me.

for(int i=0; i < rocketPosition.Count; i++)
{
    rocketPosition[i] = new Vector2(rocketPosition[i].X, rocketPosition[i].Y - rocketSpeed);
    Vector2 smokePosition = rocketPosition[i];
    smokePosition.X += Rocket.Width / 2 + smokeTexture.Width / 2 + randomizer.Next(10) - 5;
    smokePosition.Y += Rocket.Height + randomizer.Next(10) - 5;
    smokeList.Add(new Particle(smokePosition, gameTime.TotalGameTime.Milliseconds));
    if (rocketPosition[i].Y < 0 - Rocket.Height)
    {
        rocketPosition.RemoveAt(i);
    }
}

for(int i = 0; i < smokeList.Count; i++)
{
    if (smokeList[i].Time < gameTime.TotalGameTime.Milliseconds - smokeDuration)
    {
        smokeList.RemoveAt(i);
    }
}

Particle is a class I created in order to have both the creation-time of the list item (which represents the smoke particle), as well as its position Vector2.

However instead of deleting the smoke trail from bottom to top it stops in between and looks like the following picture: Smoke trail with holes in between

I hope someone can help me with my code.

Was it helpful?

Solution

Removing an item from the iterating list decrement the count therefore:

for(int i=rocketPosition.Count; i > 0 ; i--)
{
   rocketPosition[i] = new Vector2(rocketPosition[i].X, rocketPosition[i].Y - rocketSpeed);
   Vector2 smokePosition = rocketPosition[i];
   smokePosition.X += Rocket.Width / 2 + smokeTexture.Width / 2 + randomizer.Next(10) - 5;
   smokePosition.Y += Rocket.Height + randomizer.Next(10) - 5;
   smokeList.Add(new Particle(smokePosition, gameTime.TotalGameTime.Milliseconds));
   if (rocketPosition[i].Y < 0 - Rocket.Height)
   {
       rocketPosition.RemoveAt(i);
   }
}

for(int i = smokeList.Count; i  > 0 ; i--)
{
   if (smokeList[i].Time < gameTime.TotalGameTime.Milliseconds - smokeDuration)
   {
       smokeList.RemoveAt(i);
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top