The index 0 worked.
I was using an integer to represent the index, and got the message of index out of range.
Rectangle areaOfFocus = new Rectangle(mousex, mousey, 1, 1);
Color[] pixelCol = new Color[iCol];
imageFocus.GetData<Color>(0, areaOfFocus, pixelCol, 0, 1);
no error returns when I use 0, but nothing happens to my text.
if (pixelCol[0] == backgroundCol) { buttonInfo = “It Worked!”; }
Is this code correct?
Boolean pixCheck = false;
private string buttonInfo = "CLICK ME";
protected override void Update(GameTime gameTime)
{
if (mousex > imageFocus.Width - imageFocus.Width && mousex < imageFocus.Width &&
mousey > imageFocus.Height - imageFocus.Height && mousey < imageFocus.Height)
{
mousex = currentMouseState.X;
mousey = currentMouseState.Y;
// retrieve the color of the pixel beneath the mouse
Rectangle areaOfFocus = new Rectangle(mousex, mousey, 1, 1);
Color[] pixelCol = new Color[1];
imageFocus.GetData<Color>(0, areaOfFocus, pixelCol, 0, 1);
if (pixelCol[0] == backgroundCol) { buttonInfo = "It Worked!"; pixCheck = true; }
else { buttonInfo = "CLICK ME"; pixCheck = false; }
}
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(backgroundCol);
//GraphicsDevice.Clear(Color.Black); // start with a black background
if (pixCheck)
{
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
spriteBatch.DrawString(myButton_Font, buttonInfo, fontPos, Color.White, 0,
fontCenter, 1.0f, SpriteEffects.FlipHorizontally, 0f);
spriteBatch.End();
}
else
{
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
spriteBatch.Draw(myButton_START, screenCenter, null, Color.White,
0, spriteCenter, 1.0f, SpriteEffects.None, 0.5f);
spriteBatch.DrawString(myButton_Font, buttonInfo, fontPos, Color.YellowGreen, -9,
fontCenter, 1.0f, SpriteEffects.FlipHorizontally, 0f);
spriteBatch.End();
}
base.Draw(gameTime);
}