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

Author Topic: How to remove a sprite? Using lists and collision...  (Read 8591 times)

0 Members and 1 Guest are viewing this topic.

newn

  • Guest
How to remove a sprite? Using lists and collision...
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to remove a sprite? Using lists and collision...
« Reply #1 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.
Laurent Gomila - SFML developer

newn

  • Guest
How to remove a sprite? Using lists and collision...
« Reply #2 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...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to remove a sprite? Using lists and collision...
« Reply #3 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?
Laurent Gomila - SFML developer

newn

  • Guest
How to remove a sprite? Using lists and collision...
« Reply #4 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.

Xurix

  • Newbie
  • *
  • Posts: 14
    • MSN Messenger - Luixsia@crimasi.com
    • AOL Instant Messenger - Luixsia
    • Yahoo Instant Messenger - Luixsia@yahoo.com
    • View Profile
    • http://www.crimasi.com
How to remove a sprite? Using lists and collision...
« Reply #5 on: November 17, 2010, 11:19:40 pm »
SFML - Simply Fun on Many Levels

newn

  • Guest
How to remove a sprite? Using lists and collision...
« Reply #6 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How to remove a sprite? Using lists and collision...
« Reply #7 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;
}
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
How to remove a sprite? Using lists and collision...
« Reply #8 on: November 20, 2010, 02:27:36 am »
Or even easier ;)
Code: [Select]
sprites.remove_if(&hit);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything