0
votes

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.

1
Something else to watch out for in your current code. You add a new nest whenever the current state of the mouse has the left button pressed. If your game is running at 60fps this will add 60 nests if the user holds down the button for 1 second. You should store the previous state of the mouse and make sure that the previous state was not pressed and the current state is pressed. This change will only add nests once per click. - Bradley Uffner

1 Answers

0
votes

Edit: Ignore what I've posted and removed. After a second look at the question I realized there were some issues with my answer.

When you create a new stationaryFood in your Update method, you never assign the new stationaryFood's CharacterImage. You should have a Texture member in your Game1 called foodImage. You'll load the foodImage's texture in the LoadContent method. Now anytime you create new stationaryFoods in your Update, you need to assign the new food's CharacterImage with foodImage and the position you created via mouse position.

So lets say your stationaryFood class looks like:

class stationaryFood
{
    public Texture2D CharacterImage { get; set; }
    public Rectangle Position { get; set; }

    public stationaryFood(Texture2D image, Rectangle position) {
        CharacterImage = image;
        Position = position;
    }
}

So a texture member scoped to Game1:

Texture2D foodImage;

In your LoadContent method:

foodImage = Content.Load<Texture2D>("path to texture");

In Update:

if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
{
    int foodWidth = 50;
    int foodHeight = 50;

    int X = mouseStateCurrent.X;
    int Y = mouseStateCurrent.Y;

    var foodPosition = new Rectangle(X, Y, foodWidth, foodHeight);
    var food = new stationaryFood(foodImage, foodPosition);

    // no need to scope foodPosition or food to Game1 since were creating and adding to list here

    foodList.Add(food);            
}

In Draw:

foreach (stationaryFood food in foodlist)
{
    spriteBatch.Draw(food.CharacterImage, food.Position, food.Color);
}

Now I'm guessing foodList is a member of Game1 so there's no need to pass the list into Draw. If that's not the case though then pass away.