# Populating NPC's Outside Of Loop

This was my original question: Populating Sprites I used the following code to spawn Enemy Sprites in my game. I have 2 types that I was using and it would display one then remove it and display the other. It did this repeatedly(Imagine someone playing with a light switch and you have what is happening with the NPCs. EnemyType1 is Off and EnemyType2 is On.).Also It does not apply the current level number meaning, it does not spawn the number of enemies for the level. The number of enemies I want to spawn is the exact number of the level. I used other types of random methods( nextBoolean(), nextInt(), nextFloat() ). I showed the code to my friend and he told me that I would need to spawn them outside of my loop, but he did not know how to. So now I am trying to see if anyone on stack could help me. What I am trying to do is this:

NPC's = Enemies

I have 2 types of NPC's that I want to use. The number of Enemies that are spawned are equal to the the current level number. I want to be able to spawn how ever many is needed for that level and have it to where which Enemy that will spawn is random, so you don't know if you will get EnemyType1 or EnemyType2 or a Mix. Ex: Level 1, only 1 Enemy spawns, the type is random. Level 2, only 2 Enemies spawn, the type is random. Level 3, only 3 enemies spawn, the type is random. etc.

-
Your use of the word "sprite" were you might mean enemy, or NPC is making the parsing of this question extremely difficult. – AttackingHobo Jun 14 '11 at 23:03
The only sentence that describes your actual question and problem is the first and it doesn't really say much. What's the actual problem? What's your question? Please use more specific terms than "the NPCs keep switching back and forth between the various NPCs" because that doesn't really clear things up. Do their appearances keep changing? – doppelgreener Jun 15 '11 at 0:36

Basically at the start of the level you need to call a function that creates the enemies for the current level, for example you could do :

``````for(int i=0; i < currentLevel; i++)
``````

This would populate the list with a number of enemies equal to the current level. This should only be done when you change level, for example :

``````if(enemyList.Count == 0) {
currentLevel++;
CreateNewEnemies();
}
``````

This means you have to unpopulate your list when the enemies die, this will be done in your Update loop.

``````foreach(Enemy e in enemyList) {
}

enemyList.Remove(e);
}
``````

This is not optimal, but should work.

-
It's a great starting point (+1). – Randolf Richardson Jun 17 '11 at 7:09
Sorry I didn't respond sooner my computer crashed on me but I am implementing what you said. I will report back on how it went. – Manji Jun 17 '11 at 8:43
Cool I hope it helps you out. You said you weren't familiar with ArrayLists, in my answer I use a temporary list for dead enemies because if you try to delete or add an object to a list you are iterating through, you get an error. – Jonathan Connell Jun 17 '11 at 9:24

I am not 100% sure what you are asking, but from this post and the other one here is my guess:

It sounds like you were selecting which type of NPC the NPC should be during the update for the object. This would be why it constantly changes every frame.

What you need to do is find a place where you can set that value where it will only be set one time. This is probably what your friend meant by 'outside of the loop'.

If you can more clearly describe what you are seeing as opposed to trying to explain what you are doing, it might help be able to refine this answer more.

-
+1 for the last two paragraphs, although I find your interpretation of the question difficult to understand (could you improve it to make its meaning clear?): "You are selecting which NPC during the Render update so it constantly changes what it looks like every frame." – Randolf Richardson Jun 15 '11 at 2:47
@Randolf Hopefully that makes it more clear.. – James Jun 15 '11 at 16:17
Yes, it does (+1)! Thanks for taking a moment to edit that. =) – Randolf Richardson Jun 15 '11 at 16:23