SFML community forums

Help => Graphics => Topic started by: newn on November 17, 2010, 10:34:03 pm

Title: How to remove a sprite? Using lists and collision...
Post 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.
Title: How to remove a sprite? Using lists and collision...
Post by: Laurent on November 17, 2010, 10:47:03 pm
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.
Title: How to remove a sprite? Using lists and collision...
Post by: newn on November 17, 2010, 10:57:34 pm
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...
Title: How to remove a sprite? Using lists and collision...
Post by: Laurent on November 17, 2010, 11:05:31 pm
There are some useful pieces of code for collision detection on the wiki, did you have a look at them first?
Title: How to remove a sprite? Using lists and collision...
Post by: newn on November 17, 2010, 11:13:11 pm
Guess they are on the French wiki, since on the English one, there's no such tutorial.
Title: How to remove a sprite? Using lists and collision...
Post by: Xurix on November 17, 2010, 11:19:40 pm
Did you try looking under Source codes?
http://www.sfml-dev.org/wiki/en/sources?DokuWiki=62e07f9afe98156372998a17f80833a9
Title: How to remove a sprite? Using lists and collision...
Post by: newn on November 18, 2010, 02:33:13 pm
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.
Title: How to remove a sprite? Using lists and collision...
Post by: Laurent on November 18, 2010, 03:18:48 pm
Code: [Select]
for (std::list<sf::Sprite>::iterator it = sprites.begin(); it != sprites.end(); )
{
    if (hit(*it))
        it = sprites.erase(it);
    else
        ++it;
}
Title: How to remove a sprite? Using lists and collision...
Post by: Nexus on November 20, 2010, 02:27:36 am
Or even easier ;)
Code: [Select]
sprites.remove_if(&hit);