What I am attempting to do probably has a very simple answer but I can't make heads or tails of it no matter how hard I try. Provided I'm a newb.
What I am trying to do is after I create a grid of rectangles, when the cursor moves into the grid, it changes the color of the rectangle the cursor is currently in (or highlights it), then changes back when the cursor leaves. Here's what I've coded so far:
namespace MapGenie
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Rectangle[,] gameGrid;
MouseState currentMouseState;
MouseState previousMouseState;
bool colorChange = false;
Color color;
Color previousColor;
protected override void Initialize()
{
//Creates the array
gameGrid = new Rectangle[10, 10];
color = Color.Black;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
this.IsMouseVisible = true;
int Gridsize = 20;
//Creates the grid
for(int x = 0;x <10;x++)
for (int y = 0; y < 10; y++)
{
gameGrid[x, y] = new Rectangle(x * Gridsize, y * Gridsize, Gridsize, Gridsize);
}
}
protected override void Update(GameTime gameTime)
{
currentMouseState = Mouse.GetState();
var mousePoint = new Point(currentMouseState.X, currentMouseState.Y);
previousMouseState = currentMouseState;
//Checks each rectangle in the gameGrid array to see if cursor is there?
foreach (Rectangle grid in gameGrid)
{
if (grid.Contains(mousePoint))
{
previousColor = color;
color = Color.Blue;
colorChange = true;
}
if (colorChange == true)
color = previousColor;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
var t = new Texture2D(GraphicsDevice, 1, 1);
t.SetData(new[] { Color.White });
foreach (Rectangle grid in gameGrid)
spriteBatch.Draw(t, grid, color);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Before I added the else statement (Which I am also aware does not work, but I am working to fix that after asking this), the entire grid would turn blue. So, I am not sure if it is the right question or not, but how do you get just the one the cursor is in to turn [color]? Appreciate any input!