Enemy spawner 'for' loop isn't working right

Understanding the language, error messages, etc.

Enemy spawner 'for' loop isn't working right

Postby BurningSushi » Sat Jun 13, 2015 2:54 pm

Hi all,

I can't for the life of me figure out why my enemy spawner isn't working properly. So basically, a Load_Enemies() function places enemies outside of the screen (using int8_t variables). As soon as enemy[].update is true, the enemy will start walking towards the player and become collidable etc.

The 'for' loop is supposed to go through all enemies in the enemy[] array, then find a set number (5) that have the previous variable set to 'false', then stop when the number is reached and break out of the loop.

Problem is the number of enemies spawned shows as being more than is actually spawned, for example it will spawn 3 enemies when enemy_spawner.spawn_step shows 5, then 7 when it is 10 and so on. Any ideas where I should be looking?

Code: Select all
class Enemy
{
   public:
      bool update = false;
};

class Enemy_Spawner
{
   public:
      byte timer = 40;
      byte delay = 80;
      byte spawned = 0;
      byte limit = 5;
      bool spawning = false;
      byte spawn_step = 0;
};

Enemy_Spawner enemy_spawner;
Enemy enemy[30];

//Gets triggered in loop()
void UpdateEnemySpawner()
{
   enemy_spawner.timer++;
   if(enemy_spawner.timer > enemy_spawner.delay && !enemy_spawner.spawning)
   {
      enemy_spawner.timer = 0;
      enemy_spawner.spawning = true;
      for(byte i=0; i<NUM_ENEMIES; i++)
      {
         if(enemy[i].update == false)
         {
            enemy[i].update = true;
            enemy_spawner.spawned++;
            enemy_spawner.spawn_step++;
            if(enemy_spawner.spawn_step >= 5)
            {
               enemy_spawner.spawning = false;
               enemy_spawner.spawn_step = 0;
               enemy_spawner.timer = 0;
               break;
            }
         }
      }
   }
}
User avatar
BurningSushi
 
Posts: 34
Joined: Tue Jun 02, 2015 10:33 pm

Re: Enemy spawner 'for' loop isn't working right

Postby BurningSushi » Sun Jun 14, 2015 12:45 pm

One of those days: EVERYTHING was wrong

OK so the problem was that I'd mixed up the height and width of all of the spawning points, all of the problems related back to where the enemies spawned outside of the screen, the for loop was in fact right. Classic mistake XD
User avatar
BurningSushi
 
Posts: 34
Joined: Tue Jun 02, 2015 10:33 pm


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 87 guests