0
votes

I am making a game in XNA where a player moves around the screen 'collecting' kiwis (the animal, not the fruit). I want to make it so that when the player collides with a kiwi, the kiwi sprite disappears.

So far, I can only make all the kiwis disappear when a collision occurs. I only want each individual sprite to disappear in a collision. I am using the same sprite for all the kiwis.

Here is the collision function:

void Collision()
{
     if (!isCollected)
     {
          foreach (Rectangle kiwi in kiwiRectangle)
          {
              if (kiwi.Intersects(Sprite.spriteDestinationRectangle))
              {
                  isCollected = true;


              }
           }
      }


}

kiwiRectangle is an array holding rectangles that are created around each of the kiwi sprites that is drawn.

And then in the Draw() function:

if (!isCollected)
{
      foreach (Vector2 position in kiwiPosition)
      {
           spriteBatch.Draw(kiwi, position, Color.White);
      }
}
3
You need to track which kiwi is collected. You're going through all of them in a loop, so you need to track the one corresponding to when you set the flag to true.Brian Rasmussen

3 Answers

0
votes

you should make isCollected a property of Kiwi so each Kiwi has it's own isCollected.

If you do so, you need to get rid of bot (!isCollected). For the Collision you can insert something like this if you make isCollected a prop of Kiwi:

      foreach (Rectangle kiwi in kiwiRectangle)
      {
          if (!kiwi.isCollected && kiwi.Intersects(Sprite.spriteDestinationRectangle))
          {
              isCollected = true;


          }
       }

Since I can't tell how kiwiPosition is built up I can't give you advise how to solve the Draw.

0
votes

Using single sprite is right decision. XNA clearly allocates two methods - Update and Draw, as it were hinting that the data and their presentation are two different things.

Why do you need a flag isCollected? Create a small class that will keep only individual data of each kiwi

class Kiwi
{
     public Vector2 position;

     public bool Intersect (Vector2 pPlayerPosition)
     {
         // Return true if this kiwi's rectangle intersects with player rectangle
     }
}

And use like this

List <Kiwi> kiwis;
Vector2 playerPosition;
Texture2D kiwiTexture;
Initialize ()
{
     this.kiwis = new Kiwi ();
     // Add some kiwi to collection
}

LoadContent ()
{
     this.kiwiTexture = Content.Load <Texture2D> (...);
}

Update (GameTime pGameTime)
{
     if (! this.isGameOwer)
     {
         // Update playerPosition
         playerPosition = ...

         // Then check collisions
         for (int i = 0; i <this.Kiwis.Count ;)
         {
             if (this.Kiwis [i]. Intersect (playerPosition))
             {
                 this.Kiwis.RemoveAt (i);
                 / / Add points to score or whatever else ...
             }
             else
             {
                 i + +;
             }
         }

         if (this.Kiwis.Count == 0)
         {
             // All kiwis colelcted - end of game
             this.isGameOwer = true;
         }
     }
}

Draw (GameTime pGameTime)
{
     if (! this.isGameOwer)
     {
         this.spritebatch.Begin ();

         // Draw kiwis
         for (int i = 0; i <this.Kiwis.Count ;)
         {
             Vector2 kiwiDrawPosition = new Vector2 (
                 this.Kiwis [i]. position.X - kiwiTexture.Width * 0.5f,
                 this.Kiwis [i]. position.Y - kiwiTexture.Height * 0.5f);
             this.spritebatch (
                 this.kiwiTexture,
                 kiwiDrawPosition,
                 Color.White);
         }

         // Draw player
         ...

         this.spritebatch.end ()
     }
}
0
votes

I would give each Kiwi an isAlive boolean. When the collision takes place set the isAlive boolean of the specific Kiki to false, then in the draw/update method loop through all the Kiwi's and only draw/update them if isAlive = true.