samedi 27 avril 2019

Can you convert these 2 IfElse Statements to 1

I try to shorten my if statements. I completed 1 but aren't able to see how to shorten the other 1 and clean it up. I Have shortened 1 as an example and I hope I can get some help :D.

I fixed this 1.

if (_clickDestination.Y < Position.Y && 
_clickDestination.Y != Position.Y && Position.X >= _clickWalkStairsX)
            {
                Position.Y -= (int)StairSpeed;  // moves the person up on the stairs.
            }
            else if (_clickDestination.Y != Position.Y && Position.X >= _clickWalkStairsX)
            {
                Position.Y += (int)StairSpeed; // moves the person down on the stairs
            }

To this:

if (_clickDestination.Y != Position.Y && Position.X >= _clickWalkStairsX)
            {
                if (_clickDestination.Y < Position.Y)
                {
                    Position.Y -= (int)StairSpeed;  // moves the person up on the stairs.
                }
                else
                {
                    Position.Y += (int)StairSpeed;  // moves the person down on the stairs
                }
            }

Now I am trying to fix this 1 just like I did above.

else if (
                    (   _clickDestination.Y == Position.Y && 
                        _clickDestination.X > Position.X) || 
                    (   _clickDestination.Y != Position.Y &&
                        _clickWalkStairsX != Position.X)
                    ) 
            {
                Position.X += (int)Speed; // moves the person to the right
            }
            else if (   _clickDestination.Y != Position.Y || 
                        _clickDestination.X < Position.X)
            {
                Position.X -= (int)Speed; // moves the person to the left
            }

I hope someone can actually see how it needs to get fixed and would really appreciate it. ALl the if else will stand after each other.

Aucun commentaire:

Enregistrer un commentaire