入侵者在远离屏幕时不会向下移动

时间:2012-04-17 13:17:57

标签: xna

当他们到达屏幕的最左边时,我无法向下移动入侵者,但是当他们到达最右边时,他们没有遇到向下移动的麻烦。该运动的代码如下所示:

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

我不知道什么导致外星人在向左转时不会向下移动。谢谢。

1 个答案:

答案 0 :(得分: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);
  }
}
相关问题