I am doing a Space Invaders game for a project in SFML, and I have had problem, because I check the collision of the Bullet with the Enemy and it works as it should, until there is one Bullet type object inside the container, when there are already two or more and a collision occurs, I am getting the error "Unhandled Exception at 0x00007FFF7BA9DD7E (ucrtbase.dll) in SI.exe: Fatal Exit Request.". I suspect the problem is with the iterator here as the amount of objects in the container is variable.
void Bullet::hit(vector<Enemy>& enemies,vector<Pixel>& oneShield, vector<Bullet>& bullets, int &killedEnemies, int shotsFired) {
for (auto& enemy : enemies) {
if (enemy.shape.getGlobalBounds().intersects(this->shape.getGlobalBounds())) {
for (auto i = enemies.begin(); i < enemies.end(); i++) {
if (i->ID == enemy.ID) {
enemies.erase(i);
break;
}
}
killedEnemies = killedEnemies + 1;
//Problem with this loop below
for (auto j = bullets.begin(); j < bullets.end(); j++) {
if (j->ID == this->ID) {
bullets.erase(j);
break;
}
}
}
}