I'm creating a simple asteroids game and I have three lists with: big asteroids, small asteroids and missiles. When the player hits a big asteroid two small asteroids are created. But The problem is that for the most of the time, the two small asteroids never get time to show up! It seems like the missile hits them too, despite that I remove the missile from the list, before the small asteroids are created. The code below is from my object manager.
What is wrong and how can I improve my code to avoid this?
// Check if missile hit big asteroid
public void CollisionControlMissileHitAsteroidBig(ContentManager content)
{
for (int i = 0; i < missilesList.Count(); i++)
{
for (int ii = 0; ii < asteroidsBigList.Count(); ii++)
{
if (missilesList.ElementAt(i).Bounds().Intersects(asteroidsBigList.ElementAt(ii).Bounds()))
{
missilesList.RemoveAt(i);
for(int x = 0; x < 2; x++)
AddNewSmallAsteroidToList(new AsteroidSmall(content, asteroidsBigList.ElementAt(ii).Position));
asteroidsBigList.RemoveAt(ii);
i--;
ii--;
}
}
}
}
// Check if missile hit small asteroid
public void CollisionControlMissileHitAsteroidSmall()
{
for (int i = 0; i < missilesList.Count(); i++)
{
for (int ii = 0; ii < asteroidsSmallList.Count(); ii++)
{
if (missilesList.ElementAt(i).Bounds().Intersects(asteroidsSmallList.ElementAt(ii).Bounds()))
{
missilesList.RemoveAt(i);
asteroidsSmallList.RemoveAt(ii);
i--;
ii--;
}
}
}
}
EDIT:
I'm not sure I have done right? There are some problems, the big asteroids is still on the screen and then I guess the missile is still there to, because when two new small asteroids are created, most of the time there is only one, which I think is because it collide with the old missile? That's why I removed the missile from the list before I created the new small asteroids. More help is appreciated!
public void CollisionControlMissileHitAsteroidBig(ContentManager content)
{
for (int i = 0; i < missilesList.Count(); i++)
{
for (int ii = 0; ii < asteroidsBigList.Count(); ii++)
{
if (missilesList.ElementAt(i).Bounds().Intersects(asteroidsBigList.ElementAt(ii).Bounds()))
{
//missilesList.RemoveAt(i);
for(int x = 0; x < 2; x++)
AddNewSmallAsteroidToList(new AsteroidSmall(content, asteroidsBigList.ElementAt(ii).Position));
asteroidsBigListRemove.Add(asteroidsBigList.ElementAt(ii));
missilesListRemove.Add(missilesList.ElementAt(i));
}
}
}
asteroidsBigListRemove.Clear();
missilesListRemove.Clear();
}
