user1275084
user1275084

Reputation: 61

Invaders not moving down when they reach far left off screen

I am having trouble in moving down the invaders when they reach the far left of the screen, however they are having no trouble in moving down when they reach the far right. The code for the movement is shown below:

if  (Invaders[0].GetXPos() < 100) // if the first invader element riches the far left of the screen, change direction, and move down four pixels.


{
        AlienDirection = +1;

        for (int Count = 0; Count < 11; Count++)
        {
            Invaders[Count].MoveVertical(4);
        }
    }

    if (Invaders[10].GetXPos() > 924)
    {
        AlienDirection = -1;

        for (int Count = 0; Count < 11; Count++)
        {
            Invaders[Count].MoveVertical(4); // if the first invader element riches the far left of the screen, change direction, and move down four pixels.
        }
    }

I don't know whats causing the aliens from not moving down when they gear to the left. Thank you.

Upvotes: 0

Views: 101

Answers (1)

Colin D
Colin D

Reputation: 5661

My best guess is that you magic number '924' is wrong.

This could all be refactored to the following

if(Invaders[0].GetXPos() < 100 || Invaders[10].GetXPos() > 924) 
{
  AlienDirection = 0 - AlienDirection; // turns -1 into 1 and 1 into -1
  for(int Count = 0; Count < 11; Count++)
  {
    Invaders[Count].MoveVertical(4);
  }
}

Upvotes: 1

Related Questions