Ok thanks! Now a couple more questions:
Why does my code crash when i try to do this:
In my game loop I load waves of enemies like this:
Code:
void Logic::LoadWaves (void)
{
BlankCounter++;
if ((BlankCounter > 50) && (WaveCounter == 1))
{
enemies.AddEnemyToVector (2);
WaveCounter =2;
}
}
BlankCounter is the number of frames being rendered and enemies is my storage class for my vector. The AddEnemyToVector looks like this:
Code:
void EnemyList::AddEnemyToVector (int NumberToSpawn)
{
for (int i = 2; i < NumberToSpawn; i++)
{
venemyVector.push_back (new Enemy); // add new unit to array
venemyVector[i]->SpawnEnemy (i); // spawn enemy unit
}
}
Number to spawn is the number of enemies to spawn per wave, this all works fine, however when i try to remove them like this:
Code:
void EnemyList::KillEnemy (int Collided)
{
ExplosionX = venemyVector[Collided]->GetLeft ();
ExplosionY = venemyVector[Collided]->GetTop ();
Boom.LoadGFX (); // load graphics for explosion
Boom.Bang ((ExplosionX - 16), (ExplosionY - 16)); // run animation
venemyVector[Collided]->Die (Collided); //make the unit explode
venemyVector.erase (venemyVector.begin () + Collided); // erase unit from vector
}
Its the two bold lines at the bottom which aren't right, it sometimes works when i kill the last spawning unit first but sometimes one of the units remains when i call delete on it, it doesn't move or anything, i just get the random explosion when it collides with my player. Anyone got any ideas what it could be? Do I need to swap to the end of the vector before i delete it or something? If so what is the code to do that?