【问题标题】:Finding in an intersection of a rectangle in a iterating list在迭代列表中查找矩形的交集
【发布时间】:2013-05-08 09:55:01
【问题描述】:

我在尝试查找矩形的交点时遇到问题。我有一个在 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);

【问题讨论】:

  • 尝试阅读您提供给我们的代码,因为没有您的完整代码,也不知道您到目前为止做了什么。你能看出它有多不清楚吗?
  • @YoryeNathan 对此感到抱歉。让我更新代码。
  • @YoryeNathan 这更清楚吗?还是我应该添加更多?
  • “迭代发生得如此之快以至于无法检测到碰撞”是什么意思?一旦你点击鼠标,你不会只是遍历你的矩形并检查每一个,如果鼠标在一个矩形中,那么做你想做的任何事情。
  • 看起来您的 LeftClicked 例程实际上并未检查交叉点/碰撞?

标签: c# c#-4.0 xna xna-4.0


【解决方案1】:

正如 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;
                    }
                }
            }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-27
    • 2019-06-20
    • 2022-01-20
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多