我在尝试查找矩形的交叉点时遇到问题。我有一个矩形列表,在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);
答案 0 :(得分:0)
正如Paul0712所提到的,我应该检查一下,然后检查交叉点。
更新的代码:
public void Update(MouseInput mouse)
{
if (mouse.LeftClicked())
{
for (int i = 0; i < drawTangle.Count; i++)
{
if (mouse.clickRectangle.Intersects(drawTangle[i]))
{
diseaseToInfo = listOfInfo[i];
isDrawingList = false;
moreInfo = true;
}
}
}
}