SFML community forums
Help => Graphics => Topic started by: newn on November 17, 2010, 10:34:03 pm
-
Hi everyone. I've made a topic about how to draw a few lines of similar enemies. The answer was - using lists. So now I'm wondering: how could I remove a particular sprite (the one, which has been hit by the shoot) from the screen? The list counts from 1 to x and fills in. And in the first place, I've no idea on how to remove the sprite, and am unable to find the information.
Thanks.
-
So basically, you don't know how to remove an element from a list, right?
If so, maybe you should spend more time learning C++ basics instead of jumping right into graphical stuff -- which nobody should try before being comfortable with the language and its standard library, at least.
If your problem is not to remove the sprite from the list, then maybe you should give use more details.
-
I know remove and remove if... So the only idea how to remove a particular sprite, which is hit by another sprite, is this, which I guess is very inefficient:
bool place (const int& value) { return (SOMETHING!@#); }
list.remove_if (place);
And that something is a big code to check if the laser hits a particular place, and then check if the element is on the particular place (pixels. Like... check, if the shot hits the sprite on say... 150 x and 200 y.)
So I guess is that this is an inefficient way to do it. That's why I'd like to know a better way to do it...
-
There are some useful pieces of code for collision detection on the wiki, did you have a look at them first?
-
Guess they are on the French wiki, since on the English one, there's no such tutorial.
-
Did you try looking under Source codes?
http://www.sfml-dev.org/wiki/en/sources?DokuWiki=62e07f9afe98156372998a17f80833a9
-
Okay, collision... But how do I remove the sprice IF it's hit? How to remove the sprite after? Undraw.Sprite();? Certainly not. So how can I remove the sprite?
Thanks.
-
for (std::list<sf::Sprite>::iterator it = sprites.begin(); it != sprites.end(); )
{
if (hit(*it))
it = sprites.erase(it);
else
++it;
}
-
Or even easier ;)
sprites.remove_if(&hit);