0

当入侵者到达屏幕的最左侧时,我无法向下移动它们,但是当它们到达最右侧时,它们向下移动却没有问题。运动的代码如下所示:

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

我不知道是什么导致外星人在他们向左齿轮时不向下移动。谢谢你。

4

1 回答 1

1

我最好的猜测是你的幻数“924”是错误的。

这都可以重构为以下

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);
  }
}
于 2012-04-17T13:26:51.570 回答