Hello... So, I've been developing a 2D game in SFML 2.0 in C++. I got to the part where there is a collision between the player laser and the enemies. Both, the lasers and the enemies are declared as lists. When the laser hits the enemy, the collision check must be performed and if there is a collision, the laser and the enemy are erased from the list. I managed to find what is causing the problem in the collision code, but I am not able to find a solution for it. Here's my code.
std::list<sf::Sprite>::iterator enemyit = enemy.begin(), next;
std::list<sf::Sprite>::iterator greenlaserit = greenlaser.begin(), reload;
while(enemyit != enemy.end())
next = enemyit;
while(greenlaserit != greenlaser.end())
reload = greenlaserit;
greenlaserit = reload;
enemyit = next;
Here, there are two while loops and what I am doing is I am using the inner loop to check for collision and if collision occurs, I am erasing the enemy iterator. And after doing that, the inner loop will keep looping until the while condition stops being satisfied. It won't enter the outer loop until it finishes that. Problem is, the enemy iterator is being erased in the inner loop and I am not incrementing it until the program goes back to the outer loop. How do I do that so that the enemy iterator is incremented in the inner loop as soon as it is erased. I've tweaked around a lot moving code around inside and outside the loops, adding new variables, still nothing. Please help me. Thanks.