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

Author Topic: Deleting Sprites  (Read 4311 times)

0 Members and 1 Guest are viewing this topic.

nullGrind

  • Newbie
  • *
  • Posts: 23
    • View Profile
Deleting Sprites
« on: February 01, 2010, 04:43:02 pm »
Hello!

Does anybody know how to delete sprites? Id like to delete shots after they left the screen.
I looked into the documentation but couldnt find a sprite-method to delete them.

The sprites are saved in a vector but it seems that i cant use vectormethods to delete elements. it seems that i can only use spritemethods.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Deleting Sprites
« Reply #1 on: February 01, 2010, 04:45:15 pm »
You don't need anything specific, just destroy your sprite instance if you don't need it anymore (remove it if it's stored in a container, delete it if it's a pointer, etc.).
Laurent Gomila - SFML developer

nullGrind

  • Newbie
  • *
  • Posts: 23
    • View Profile
Deleting Sprites
« Reply #2 on: February 01, 2010, 04:46:41 pm »
The sprites are saved in a vector but it seems that i cant use vectormethods to delete elements. it seems that i can only use spritemethods.

main.cpp:169: error: 'class sf::Sprite' has no member named 'erase'

Code: [Select]
       int j = 0;
        for (j = 0; j < shotVector.size(); j++) {
            shotVector[j].Move(-speedShot * sin(directionVector[j] * radian), -speedShot * cos(directionVector[j] * radian));
            if(shotVector[j].GetPosition().x < -50 || shotVector[j].GetPosition().x > width
                    || shotVector[j].GetPosition().y < 100 || shotVector[j].GetPosition().y > height) {
                shotVector[j].erase(shotVector.begin()+j);    //  <----- LINE 169
            }
        }

chipsman

  • Newbie
  • *
  • Posts: 23
    • View Profile
Deleting Sprites
« Reply #3 on: February 01, 2010, 04:54:18 pm »
You shoud do :
Code: [Select]

shotVector.erase(shotVector.begin() + j);


Doing shotVector[j], you access to the contained sf::Sprite. So,  function 'erase' is not found.

nullGrind

  • Newbie
  • *
  • Posts: 23
    • View Profile
Deleting Sprites
« Reply #4 on: February 01, 2010, 04:56:13 pm »
Thank you chipman and merci beacoup Laurent.
It works.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Deleting Sprites
« Reply #5 on: February 01, 2010, 04:57:04 pm »
And you must not increment j when you erase an element, otherwise you'll skip an element.
Laurent Gomila - SFML developer

nullGrind

  • Newbie
  • *
  • Posts: 23
    • View Profile
Deleting Sprites
« Reply #6 on: February 03, 2010, 03:49:35 pm »
Now i created a sprite with sf::Sprite which is not saved in a vector. How can i delete it or remove it from the screen?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Deleting Sprites
« Reply #7 on: February 03, 2010, 03:54:46 pm »
Just don't draw it.
Laurent Gomila - SFML developer

nullGrind

  • Newbie
  • *
  • Posts: 23
    • View Profile
Deleting Sprites
« Reply #8 on: February 03, 2010, 04:31:45 pm »
mhhh sure. I only need a bool trigger to draw it or not. Just too easy. Didnt thought about it  :lol: