for-in-loop/ Condition 仅用于 a List 的第一个元素

发布于 2024-11-27 13:10:54 字数 1592 浏览 0 评论 0原文

我已经搜索了三天,没有找到解决方案,代码如下:

if (keyboardState.IsKeyDown(Keys.Right))
        {
            for (int i = GlobalClass.BlocksPositions.Count - 1; i > 0; i--)
            {
                if (new Rectangle((int)GlobalClass.BlocksPositions[i].X, (int)GlobalClass.BlocksPositions[i].Y, bT.Width, bT.Height).Intersects(new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height)))
                {

                    c = 0;
                }
                else
                {
                    c = 1;
                }
            }

            if (c == 1)
            {
                Position.X += Speed;
            }

        }

每个块位置等于我可以通过单击屏幕创建的块,然后将新块位置放入列表中。基本上我的 BlockPosition 列表中有一个块坐标列表。然后我传递每个块位置的条件,条件为每个块位置创建一个矩形,并为玩家创建一个矩形...如果发生碰撞,玩家将不会朝该方向移动。当我尝试代码时,我的角色将仅与列表的第一个元素碰撞,而不与其他元素碰撞,如果我删除第一个元素,它将与下一个元素碰撞,但不会与其他元素碰撞。所有变量都很好我知道这一点,因为我尝试用这样的代码替换此代码:

if (keyboardState.IsKeyDown(Keys.Right))
        {
            for (int i = GlobalClass.BlocksPositions.Count - 1; i > 0; i--)
            {
                if (new Rectangle((int)GlobalClass.BlocksPositions[i].X, (int)GlobalClass.BlocksPositions[i].Y, bT.Width, bT.Height).Intersects(new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height)))
                {
                    GlobalClass.BlocksPositions.RemoveAt[i];

                }
            }
        }

同样的事情,但在这里如果它发生冲突,我会删除列表的元素,这是相同的条件,但当我尝试它时,它会检测到所有元素并删除我触摸的元素。我尝试了 foreach 函数,得到了相同的结果。怎么了?我已经用变量做了很多事情,所以我确信问题不是来自它们的值,而是来自我对它们所做的事情。请帮忙! (:

I have searched for three days and didn't find a solution, Here is the code:

if (keyboardState.IsKeyDown(Keys.Right))
        {
            for (int i = GlobalClass.BlocksPositions.Count - 1; i > 0; i--)
            {
                if (new Rectangle((int)GlobalClass.BlocksPositions[i].X, (int)GlobalClass.BlocksPositions[i].Y, bT.Width, bT.Height).Intersects(new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height)))
                {

                    c = 0;
                }
                else
                {
                    c = 1;
                }
            }

            if (c == 1)
            {
                Position.X += Speed;
            }

        }

Each Block position equals a block that I can create by clicking on the screen, the new block position is then put in the List. Basically I have a list of blocks Coordinates in my BlockPosition List. Then I pass the condition for each blockposition, the Condition Create A rectangle for each BlockPosition and one for the Player... if there's a collision, the player won't move in that direction. When I try the code, My character will Collide only with the first element of the List and not the others, if I delete the first element it will then collide with the next one but not the others. All the variables are FINE I know it because I tried to replace this code by something like this:

if (keyboardState.IsKeyDown(Keys.Right))
        {
            for (int i = GlobalClass.BlocksPositions.Count - 1; i > 0; i--)
            {
                if (new Rectangle((int)GlobalClass.BlocksPositions[i].X, (int)GlobalClass.BlocksPositions[i].Y, bT.Width, bT.Height).Intersects(new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height)))
                {
                    GlobalClass.BlocksPositions.RemoveAt[i];

                }
            }
        }

Same thing but here if it collides I delete the Element of the List, it's the same condition but when I try it it will detect all of the elements and delete the ones that I touch. I tried the foreach function and I get the same Results. What's wrong? I already do a lot of things with does variables So I'm sure the problem don't come from their values but with what I do with them. Help please! (:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小镇女孩 2024-12-04 13:10:54

发现我必须像这样打破循环:

if (keyboardState.IsKeyDown(Keys.Right))
    {
        for (int i = GlobalClass.BlocksPositions.Count - 1; i > 0; i--)
        {
            if (new Rectangle((int)GlobalClass.BlocksPositions[i].X, (int)GlobalClass.BlocksPositions[i].Y, bT.Width, bT.Height).Intersects(new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height)))
            {

                c = 0;
                break;
            }
            else
            {
                c = 1;
            }
        }

        if (c == 1)
        {
            Position.X += Speed;
        }

    }

Found out I have to break the loop like this:

if (keyboardState.IsKeyDown(Keys.Right))
    {
        for (int i = GlobalClass.BlocksPositions.Count - 1; i > 0; i--)
        {
            if (new Rectangle((int)GlobalClass.BlocksPositions[i].X, (int)GlobalClass.BlocksPositions[i].Y, bT.Width, bT.Height).Intersects(new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height)))
            {

                c = 0;
                break;
            }
            else
            {
                c = 1;
            }
        }

        if (c == 1)
        {
            Position.X += Speed;
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文