Cómo restablecer una lista XNA y C #
Pregunta
Necesito hacer una opción de "reintentar" cuando el jugador termine el juego. Para hacerlo, pensé que pensé en restablecer las listas de monstruos y otros objetos que se movían en la primera jugando o que se han "matado". Por ejemplo, yoTener una lista como esa:
//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;
}
}
}
}
Se deben extraer de nuevo, pero si los quité, no se dibujarán. Si no los quito, en cambio, los enemigos están en diferentes lugares (porque me siguen). ¿Alguna sugerencia para restaurar o reinicializar la lista?
Solución
No estoy seguro de si entendí tu pregunta, pero ... Al reiniciar el juego, simplemente podrías vaciar la lista
enem1.Clear();
y luego llévelo como lo hagas en el primer inicio del juego:
enem1.Add(new enemy1(....));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow