尝试查找矩形的交点时遇到问题。我有一个在 for 循环中被绘制到屏幕上的矩形列表。我想找出鼠标是否与它们中的任何一个相交,但我认为问题在于迭代发生得太快以至于无法检测到碰撞。有没有更好的方法在矩形列表中找到交点?
我试过的代码:
for (int i = 0; i < drawTangle.Count; i++)
{
mouse.Update(gameTime);
if (mouse.clickRectangle.Intersects(drawTangle[x]) && mouse.LeftClicked())
{
info = listOfInfo[x];
isDrawingList = false;
moreInfo = true;
}
}
和
mouse.Update(gameTime);
if (mouse.clickRectangle.Intersects(drawTangle[x]) && mouse.LeftClicked())
{
info = listOfInfo[x];
isDrawingList = false;
moreInfo = true;
}
x += 1;
if (x == drawTangle.Count)
x = 0;
但是这些都不会检测到交叉点(至少对我们慢人来说)。
有什么建议么?
变量:
Dictionary<string, Information> infoList;
List<string> listOfInfo;
List<Rectangle> drawTangle;
绘图代码:
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(window, Vector2.Zero, Color.White);
if (isDrawingList)
{
for (int i = 0; i < drawTangle.Count; i++)
{
spriteBatch.Draw(button, drawTangle[i], Color.White);
spriteBatch.DrawString(font, infoList[listOfInfo[i]].name, new Vector2(drawTangle[i].X + 8, drawTangle[i].Y + 4), Color.Black);
}
}
else if (moreInfo)
{
spriteBatch.DrawString(font, infoList[info].name, new Vector2((GraphicsDeviceManager.DefaultBackBufferWidth / 2) - infoList[listOfInfo].name.Length / 2, 4), Color.Black);
}
}
鼠标方法:
public bool LeftClicked()
{
if (currentmouse.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed
&& oldmouse.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Released)
{
clickedonce = true;
time = gametime.TotalGameTime.TotalSeconds + .5f;
return true;
}
else
{
return false;
}
}
和
clickRectangle = new Rectangle((int)Position.X, (int)Position.Y, 3, 3);