简体   繁体   中英

Drawing Rectangles from a List

I am trying to create some basic rectangles on my map.

This is my MapBounderies class:

public int boundY, boundX, boundWidth, boundLength;
    public Texture2D rectTexture;
    public Rectangle boundRectangle;

    List<MapBounderies> boundsList = new List<MapBounderies>();

    public MapBounderies(Rectangle bRectangle, int intX, int intY, int intWidth, int intLength)
    {
        boundY = intY;
        boundX = intX;
        boundWidth = intWidth;
        boundLength = intLength;
    }

    public void Load(ContentManager Content)
    {
        rectTexture = Content.Load<Texture2D>("black colour");
    }

    public void AddToList(SpriteBatch spriteBatch)
    {
        boundsList.Add(new MapBounderies(new Rectangle(), 100, 100, 100, 100));
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        foreach (MapBounderies boundries in boundsList)
        {
            spriteBatch.Draw(rectTexture, new Rectangle(boundries.boundX, boundries.boundY, boundries.boundWidth, boundries.boundLength), Color.White);
        }
    }

And this is the error I am getting in my main game code when I try to create a new "MapBounderies":

MapBounderies mapBounderies = new MapBounderies();

There is no argument given that corresponds to the required formal parameter 'bRectangle' of 'MapBounderies.MapBounderies(Rectangle, int, int, int, int)'

I am new to xna and any help would be appreciated.

And this is the error I am getting in my main game code when I try to create a new "MapBounderies"

There is no argument given that corresponds to the required formal parameter 'bRectangle' of 'MapBounderies.MapBounderies(Rectangle, int, int, int, int)'


The reason for this error is because you don't have a default constructor, the only signature you have from the code you provided is:

 public MapBounderies(Rectangle bRectangle, int intX, int intY, int intWidth, int intLength)

As you can see, it takes 5 parameters and you don't have another one that is empty. So to fix, just create another constructor with no parameters.

 public MapBoundaries(){}

Now you can create an instance of MapBoundaries that doesn't take any parameters.

 MapBounderies mapBounderies = new MapBounderies();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM