3
« on: August 05, 2023, 05:37:30 pm »
In my game I have enemies that move down the screen, and if they hit a wall, the hp of that wall goes down and the enemy is deleted. Once the hp of the wall is 0, the wall is deleted. Everything seems to work fine until around the 5th or 6th wall is deleted. There are three rows of walls stacked in a brick-like formation, and when one of the walls in the second or third row is deleted and there still exists a wall in a row above it, that one above it is also deleted.
Here is my code:
void Wall::updateWalls(std::vector<Wall>& walls, std::vector<Enemy*>& enemies){
for (size_t j = 0; j < enemies.size(); j++) {
bool wallErased = false;
size_t i;
for (i = 0; i < walls.size(); i++) {
if (walls.wall.getGlobalBounds().intersects(enemies[j]->enemySprite.getGlobalBounds())) {
enemies.erase(enemies.begin() + j);
walls.hp--;
if (walls.hp <= 0) {
wallErased = true;
break;
}
}
}
if (wallErased) {
walls.erase(walls.begin() + i);
i--;
}
}
}