Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Messages - argh

Pages: 1 [2]
General / Collision: Erasing two sprites from two different vectors (Solved)
« on: September 19, 2015, 05:09:42 pm »
Hi everyone,

I have a problem erasing two elements when they collide. I go thru the 2 vectors of sprites and check if they have intersected with each other, if true, then I erase the two elements from the two vectors (bullets & enemies).

My problem is the following: I can erase one, but how can I erase the second one?

That is how the function looks like so far.
void collision()
          for (auto e = enemies.begin(); e != enemies.end(); e++)
                  for (auto i = bullets.begin(); i != bullets.end(); i++)
                          sf::FloatRect enemiez = e->getGlobalBounds();
                          sf::FloatRect bulletz = i->getGlobalBounds();

                          if (bulletz.intersects(enemiez))
              //enemies.erase(e); <-- if i uncomment this line i get a "vector iterator not incrementable!" exception.

I understand that it is a problem with the size and the index in the vector and erasing won't work in the same iteration of the loop. I tried also to erase the enemies(e) in the outer loop, but no good.

I have no idea how else I can solve this? Should I try a totally different approach or am I on the right track?

Thank you for your help in advance!
PS This is my first project with SFML (newest version of SFML and Visual Studio 2013).

Pages: 1 [2]