I'm having trouble with a particular area of my code. I'm trying to create a rectangle over a button, and when the cursor is inside the rectangle, the button will change to a red color.
private bool rectangleContainsPoint(
Vector2 rectanglePos,
Texture2D rectangleImage,
Point targetPoint)
{
Rectangle rect = new Rectangle((int)rectanglePos.X, (int)rectanglePos.Y,
rectangleImage.Width, rectangleImage.Height);
if (targetPoint.X < rect.X)
{
return false;
}
else if (targetPoint.Y < rect.Y)
{
return false;
}
else if (targetPoint.X > rectangleImage.Width)
{
return false;
}
else if (targetPoint.Y > rectangleImage.Height)
{
return false;
}
else
return true;
}
After looking over this a dozen times, I can't see why this doesn't work.
You should add the width to X and Height to Y
else if (targetPoint.X > rect.X + rectangleImage.Width )
{
return false;
}
else if (targetPoint.Y > rect.Y + rectangleImage.Height)
{
return false;
}