SFML community forums

Help => Graphics => Topic started by: Grownup on October 29, 2009, 11:00:47 am

Title: After Collision, Sprite Delete!
Post by: Grownup on October 29, 2009, 11:00:47 am
Hello,
   
I have a question about Collison.
And while I wish that when two objects
do with each other, a collision of
Sprite 2 is deleted. So from the screen
disappears. Only I do not know how to
which implements in the syntax.

Thanks in advance!
And excuse me for my bad
English!
Title: After Collision, Sprite Delete!
Post by: Nexus on October 29, 2009, 06:48:38 pm
Hi,

There are several approaches to your problem. If you only have two sprites, you can just stop drawing one of them (don't call sf::RenderWindow::Draw()).

But to be flexible and use more than two objects, you can use STL-containers. You store your objects in a container and check each of it for collision with others. If two objects collide, you will delete one of them (according to your logic).
Code: [Select]
std::vector<sf::Sprite> MySprites;
... // fill container here

// get iterator to the element you want to delete
std::vector<sf::Sprite>::iterator Itr = ...;
MySprites.erase(Itr);

If you aren't used to the Standard Template Library (STL), you could search some tutorials on google or have a look at http://www.cplusplus.com. Note that the STL is a really important part of the C++ standard library. It's very advisable to know it well when you want to work efficiently with SFML (this applies to many parts of C++).