Question

I need to do a "retry" option when the player finishes the game.For doing this I thought to reset the lists of Monsters and other objects that moved at the first playing or which have been "killed".for example I have a list like that:

   //the enemy1 class is already done
    // in Game1 I declare it
    List<enemy1> enem1 = new List<enemy1>();
    //Initialize method
    List<enemy1> enem1 = new List<enemy1>();
    //LoadContent
    foreach (enemy1 enemy in enem1)
    {
        enemy.Load(Content);
    }
    enem1.Add(new enemy1(Content.Load<Texture2D>("enemy"), new Vector2(5900, 12600)));

    //Update
    foreach (enemy1 enemy in enem1)
    {
        enemy.Update(gameTime);
    }
    //after being shooted the enemies disappear and i remove them
    //if the monsters are shooted the bool "visible" goes from false to true
    for (int i = enem1.Count - 1; i >= 0; --i)
         {
             if (enem1[i].visible == true)
                  enem1.RemoveAt(i);
          }
    //Draw
    foreach (enemy1 enemy in enem1)
        {
            if(enemy.visble==false)
          {
            enemy.Draw(spriteBatch, gameTime);
          }
        }
    //So my problem is to restart the game.
    if(lost==true)
    {
        //here I have to put the code that restore the list
          //I tried:
    foreach (enemy1 enemy in enem1)
        {
           enemy.visible=false;
        }
    }
   }

}

they should be drawn again but if I removed them they won't be drawn anymore.If I don't remove them ,instead, the enemies are in different places (because they follow me). Any suggestions to restore or reinitialize the list??

Was it helpful?

Solution

I'm not sure if I understood your question right but... When restarting the game you could just empty the list

enem1.Clear();

and then refill it like you do at the first start of the game:

enem1.Add(new enemy1(....));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top