开发者

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

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

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 wi开发者_运维知识库th what I do with them. Help please! (:


You don't do anything if the if evaluates to true, and you don't seem to need the c variable anyway. How about...

if (keyboardState.IsKeyDown(Keys.Right))
{
    foreach( var pos in GlobalClass.BlocksPositions.Reverse() )
    {
        var rect = new Rectangle((int)pos.X, (int)pos.Y, bT.Width, bT.Height);
        var rectToTest = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);
        if (!rect.IntersectsWith(rectToTest))
            Position.X += Speed;
        else
            break;
    }
}

If you actually do need c for some sort of state management then you can add it back in.


For readability, consider something like this:

if (GlobalClass.BlocksPositions.All(x => !DoesIntersect(Position, x))
     Position.X += speed;

Not only is the purpose easier to deduce, but the particular mistake you made just doesn't come up.


Your loop continues even when you've found an intersection. You need a break in the true clause of the if-statement.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜