Search code examples
c#visual-studiolistxna

Space Invaders stick to wall


So I have to develop an XNA game for school and I thought Space Invaders was a nice thing to make. But now I'm stuck with a problem. I have a List filled with 'Aliens' from another class. And every second it moves 20 px to the right and I want it to descend when it touches the right side of the screen (not the problem). But I want it to move the opposite direction (to the left) as soon as it touches the wall. Here lies the problem. As soon as it touches the wall it still moves 20px once, changes direction, moves 20px back and changes direction again. This keeps repeating. So where lies the problem. I think it has to check earlier if one of the aliens touches the screen but don't know how to accomplish that and thought maybe one of you could help me out because it is very frustrating!

I included the update method

if (xPos % step == 0)
{
   if (!isDescending)
   {
      for (int k = 0; k < sprites.Count; k++)
      {
         Sprite sprite = sprites[k];

         if (touchedRight) sprite.position.X += step;
         else sprite.position.X -= step;
      }

      for (int k = 0; k < sprites.Count; k++)
      {
         Sprite sprite = sprites[k];
         bool hitLeft = sprite.position.X == 0;
         bool hitRight = sprite.rect.Right == screenWidth;
         if ((hitLeft) || (hitRight))
         {
            touchedRight = !touchedRight;
            isDescending = true;
         }
      }
   }

   else
   {
      isDescending = false;
      for (int k = 0; k < sprites.Count; k++)
      {
         sprites[k].position.Y += sprites[k].rect.Height;
      }
   }
}
// CheckCollision(alienBounds, k-1);
// sprite.rect = new Rectangle((int)sprite.position.X, (int)sprite.position.Y, 20, 20);
// alienBounds = sprite.rect;
xPos += 1;

Solution

  • So i made a new rectangle called 'bounds' which is as wide as the aliens and moves the same as the aliens do. And got it working! (half of it). It moves to the right until it touches the right side, then it goes to the left but keeps going to the left and never goes back to the right. So where am i going wrong this time?

    if (xPos % step == 0)
                {
                    if (!isDescending)
                    {
    
                        for (int k = 0; k < sprites.Count; k++)
                        {
                            Sprite sprite = sprites[k];
                            sprite.position.X += step;
                        }
    
                        if (bounds.Right == screenWidth || bounds.Left == 0)
                        {
                            touchedRight = !touchedRight;
                            step *= -1;
                        }
    
                        if (!touchedRight) bounds.X += step;
                        if (touchedRight) bounds.X -= step;
    
                        // for (int k = 0; k < sprites.Count; k++)
                        // {
                        //    Sprite sprite = sprites[k];
    
                        //    bool hitLeft = sprite.position.X == 0;
                        //    bool hitRight = sprite.rect.Right == screenWidth;
                        //    if ((hitLeft) || (hitRight))
                        //    {
                        //        touchedRight = !touchedRight;
                        //        isDescending = true;
                        //    }
                        // }
                    }