I am creating an ant simulation program where ants travel around and collect food and bring it to the nest. When a certain number of ants have collected from the food I want the food source to disappear. I have a separate class for antObject, foodObject and nestObject.
I have a 'int foodleft = 25' variable in my stationaryFood class. The foodobjects are placed onto the screen when the user clicks, and are immediately added to a list object 'foodList'. A draw method draws all the objects in the list. I cannot figure out how to make the specific food object disappear when its 'foodLeft' variable hits 0!
Here is my draw method in my stationaryFood class -
public void Draw(SpriteBatch spriteBatch, List<stationaryFood> foodlist)
{
foreach (stationaryFood food in foodlist)
{
spriteBatch.Draw(foodImage, foodBoundingRectangle, foodColor); //draw each food object in foodList
}
}
And here is my attempted solution to the problem in my main 'game1' class in the update method
if (foodList.Count > 0)
{
foreach (stationaryFood f in foodList)
{
if (f.FoodLeft == 0)
{
foodList.Remove(f);
}
}
}
However this does not work! I get an 'unhandled invalid operation error'.
In my draw method in my main game1 class I have this
foreach (stationaryFood f in foodList)
{
f.Draw(spriteBatch, foodList);
}
Can anybody see where I am going wrong?