I know this question has been posted a couple of times, but I do not understand why it does not work. I am currently in a Master's Programme where we have to learn C++ and our teacher gave us this game engine he made himself to make our exercises in. This time it was a simple version of asteroids.
Now, I submitted my exercise as required, but it has a weird bug in it that even my teacher could not wrap his head around, how to fix. So I hope it is okay that I ask here.
We have the following function:
void AsteroidsGame::DestroyLaser() {
for (int i = 0; i < gameObjects.size(); i++) {
if (dynamic_cast<Laser*>(gameObjects[i].get())) {
auto itr = gameObjects.begin() + i;
gameObjects.erase(itr);
break;
}
}
}
The function is called from the Laser class whenever it collides with an asteroid, or after a set amount of time has passed, so the laser won't go on forever. It works fine when it collides with something, but as soon as it needs to disappear due to the time constraint, I get the error in the title. Every single time. I read that it might be because I am erasing the last vector element and that causes issues, but then how would I do it??
All of the elements on the screen are contained within the gameObjects vector as a GameObject class pointed to with a shared_ptr. I have checked and made sure that the itr variable points to the right element in the vectorand it does.
Aucun commentaire:
Enregistrer un commentaire