Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

Thanks In Advance.

share|improve this question
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
up vote 1 down vote accepted

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++)
    enemyList.Add(new Enemy(nextInt() % 2));

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) {

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) {

foreach(Enemy e in deadEnemies) {

This is not optimal, but should work.

share|improve this answer
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.

share|improve this answer
+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

It sounds like you need to learn more about how Object Oriented Programming works.

I think that you are creating an instance of an enemy, and then without storing that instance in an array, just creating a new instance with the same name, effectively destroying the original instance and creating a new instance.

Create an array that will hold all the enemies. When you spawn an enemy add it to next position in the array.

In your update method loop through all the enemies and update 1 per loop.

share|improve this answer
I am using an array, using an array for objects I am not to familiar with. – Manji Jun 15 '11 at 3:40
What??????????? – AttackingHobo Jun 15 '11 at 4:21
I am using an ArrayList for the enemy npc's, I am not to familiar with using an ArrayList though. – Manji Jun 15 '11 at 6:06
@Manji: If you're not familiar with ArrayList, then you should read the documentation for that class in order to become familiar with it. – Randolf Richardson Jun 17 '11 at 7:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.