I'm trying to iterate over a vector of stored std::unique_ptr<Enemy>'s to check if they're colliding with the player's attack. If they are, I want to delete the Enemy from the vector.
However when I do this, I get the error:
File: c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector Line: 102 Expression: vector iterator not incrementable
Here's the code:
if(actionCnt > 0)
{
sf::FloatRect* playerAction = player->action(actionCnt);
if (!enemies.empty()) {
for (auto&i : enemies) {
if (playerAction->intersects(i->getBox())) {
i->kill();
enemies.erase(std::remove(enemies.begin(), enemies.end(), i));
}
}
}
delete playerAction;
playerAction = NULL;
}
So essentially, if the player triggers an attack, actionCnt goes to 1 and this segment gets executed.
playerAction is a rectangle that gets moved to simulate a swing, so here I'm checking to see if the rectangle collides with an enemy which would kill it if so.
I'm using SFML libraries, but it should be obvious what the classes do.
Right now I only have 1 enemy on the field for testing this stuff. So when I hit it, the enemies vector should be empty.