What I am trying to do is create a map generator that will create a grid of rectangles, and then fill random rectangles with different properties. (For example in a grid of 2×2, 1,1 would be just a normal ‘field’ in the game, 1,2 would be a base and have different properties than one (like the ability to construct units), 2,1 would be a water tile (and not be passable by units), and 2,2 would be a mountain (similar properties of the previous). I’ve created (what I think) is a grid, but when I attempt to reference the array that I created it doesn’t work the way I’d like it to. My guess is that I am not doing it correctly, but here is some code:
namespace MapGenie
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GameMap[,] basicMap = new GameMap[10,10]; //I have this done in Initialization normally.
Rectangle[,] basicGrid = new Rectangle[10,10];
}
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
int GridSize = 20;
for(int x = 0;x <10;x++)
for (int y = 0; y < 10; y++)
{
basicMap[x, y] = new GameMap(Content.Load<Texture2D>("Textures\\Plains"));
basicMap[x, y].position.X = x * GridSize;
basicMap[x, y].position.Y = y * GridSize;
basicGrid[x, y] = new Rectangle(x, y, x * GridSize, y * GridSize);
}
}
protected override void Update(GameTime gameTime)
{
//This is what I use to test reference a specific point on the grid, however whenever the
//mouse crosses into any of the 3x3 area starting with 1,1 it closes the program.
if (mousePoint.Intersects(basicGrid[3, 3]))
this.Exit();
}
So the question is simply: How can I get the program to recognize just a single rectangle in the array?
The problem with your code is on this line:
Look at the constructor for
Rectangle(MSDN). Note that it asks for the width and height of the rectangle. You are giving it something different.