I am creating a program that simulates ants running around, collecting food and depositing it in a nest. I want the user to be able to click and add a nest object at the cursor point. I also want the object created to be added to a list of nests.
So far, I have tried this in my update method in my main game class.
mouseStateCurrent = Mouse.GetState();
if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
{
int foodWidth = 50;
int foodHeight = 50;
int X = mouseStateCurrent.X;
int Y = mouseStateCurrent.Y;
foodPosition = new Rectangle(X, Y, foodWidth, foodHeight);
food = new stationaryFood(foodPosition);
foodList.Add(food);
}
This compiles but when I click the game crashes and I get an error saying that when the food object is drawn in the 'draw' method, the texture for the food is null. I understand why this is happening, as I have tried to load in the textures as follows in the LoadContent() method in the main game class
foreach (stationaryFood f in foodList)
{
f.CharacterImage = foodImage;
}
And here is the set/ get in the separate class for the food object
public Texture2D CharacterImage
{
set
{
foodImage = value;
}
get
{
return foodImage;
}
}
Here is the method in the food object class which I get the error
public void Draw(SpriteBatch spriteBatch, List<stationaryFood> foodlist)
{
foreach (stationaryFood food in foodlist)
{
spriteBatch.Draw(foodImage, foodBoundingRectangle, foodColor);
}
}
The foodimage variable is null. I know this is because when LoadContent() loads the image there is nothing in the list at that point! But I don't know how to fix this! Its probably really simple Im just fairly new at programming! Any help would be appreciated and I hope I didn't explain it too incomprehensibly.