开发者

for-in-loop/ Condition Is only used for the first element of the a List

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! (:


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

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜