Hello,
I've been looking for an answer for the past few days, and it's been frustrating.
I have a vector array of Enemy classes:
vector<Enemy> enemies;
while (window.isOpen()) {
if (spawned == false) {
for (int i = 0; i < 5; i++) {
Enemy enemyNew(enemy);
enemyNew.setPosition(sf::Vector2f(100 + (i * 160), 100));
enemyNew.setHealth(enemyHealth);
enemies.push_back(enemyNew);
};
spawned = true;
}
}
The Enemy class has a private AnimatedSprite variable from
https://github.com/SFML/SFML/wiki/Source:-AnimatedSprite, now, once a bullet hits an Enemy, the enemy loses health, blah blah, dies. Once the enemy is dead it sets a boolean to false.
In the main window loop, it erases the Enemy from the vector if that boolean is false.
enemies.erase(std::remove_if(enemies.begin(), enemies.end(), predEnemy), enemies.end());
However, once I kill an enemy, and it is erased from the vector, it returns
vector subscript out of range
and leads me to the following function in
Animation.cpp (from the AnimatedSprite wiki):
const sf::IntRect& Animation::getFrame(std::size_t n) const
{
return m_frames[n];
}
So I have identified it isn't a fault in my code.
Also, this happens at random times, so if I launched into the game, waited a couple seconds, killed the enemy, then slowly killed all the other enemies aswell, there would be no crash. But as soon as I start firing rapidly it triggers the error. I assume this has to do with the frames of the Animation etc.
Also, no, the bullet is not animated.Thanks in advance.