0
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Your use of the word "sprite" were you might mean enemy, or NPC is making the parsing of this question extremely difficult. \$\endgroup\$ Jun 14, 2011 at 23:03
  • \$\begingroup\$ 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? \$\endgroup\$ Jun 15, 2011 at 0:36

3 Answers 3

1
\$\begingroup\$

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) {
    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) {
    if(e.IsDead)
        deadEnemies.Add(e);
}

foreach(Enemy e in deadEnemies) {
    enemyList.Remove(e);
}
deadEnemies.Clear();

This is not optimal, but should work.

\$\endgroup\$
3
  • \$\begingroup\$ It's a great starting point (+1). \$\endgroup\$ Jun 17, 2011 at 7:09
  • \$\begingroup\$ 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. \$\endgroup\$
    – Manji
    Jun 17, 2011 at 8:43
  • \$\begingroup\$ 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. \$\endgroup\$ Jun 17, 2011 at 9:24
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ +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." \$\endgroup\$ Jun 15, 2011 at 2:47
  • 1
    \$\begingroup\$ @Randolf Hopefully that makes it more clear.. \$\endgroup\$
    – James
    Jun 15, 2011 at 16:17
  • \$\begingroup\$ Yes, it does (+1)! Thanks for taking a moment to edit that. =) \$\endgroup\$ Jun 15, 2011 at 16:23
0
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ I am using an array, using an array for objects I am not to familiar with. \$\endgroup\$
    – Manji
    Jun 15, 2011 at 3:40
  • 2
    \$\begingroup\$ What??????????? \$\endgroup\$ Jun 15, 2011 at 4:21
  • \$\begingroup\$ I am using an ArrayList for the enemy npc's, I am not to familiar with using an ArrayList though. \$\endgroup\$
    – Manji
    Jun 15, 2011 at 6:06
  • \$\begingroup\$ @Manji: If you're not familiar with ArrayList, then you should read the documentation for that class in order to become familiar with it. \$\endgroup\$ Jun 17, 2011 at 7:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .