Here I am again!
Im making a tower-defense like game, and I got boids moving around, and a player that can put towers up that shot and kill the boids.. Only problem is the program sometimes crahses when a boid dies!.. I save all the boids in a common list so I do with the towers (that right now is called Obstecals) and the shots per tower is also listed...
You can see the code here:
for (int i = 0; i < positionList.Count; i++)
{
for (int oi = 0; oi < obstecalList.Count; oi++)
{
for (int x = 0; x < obstecalList[oi].shotList.Count; x++)
{
if (boundingSphereList.Intersects(obstecalList[oi].shotList[x].boundingSphere))
{
killBoid(i);
continue;
}
}
continue;
}
continue;
}
for(int i = 0; i < positionList.Count; i++){
if (positionList.Translation.X <= -1000)
{
killBoid(i);
continue;
}
}
for (int i = 0; i < positionList.Count; i++)
{
if (player.boundingSphere.Intersects(boundingSphereList))
{
killBoid(i);
continue;
}
}
A boid will die when it is shot, reaches the end of the map, or collides with the player. The killBoid function deletes the boid from the positionList, velocityList and boundingSphereList and can be seen here.
public void killBoid(int i)
{
positionList.Remove(positionList);
velocityList.Remove(velocityList);
boundingSphereList.Remove(boundingSphereList);
}
btw. I know that right now this code is somewhat messy, but im far from the cleaning up stage It will come!
The problem is apparently that when a boid gets killed the lists becomes shorter than they were before, and sometimes it will make a for-loop go out of bounds.. BUT! I don't understand why this happens, as I put a continue in to prevent this :S.. Shouldnt the continue make the program exit the loop as soon as it hits that ???? ...