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

Author Topic: How to remove a sprite from the screen? Or set transparent? SFML 2.0/C++  (Read 19205 times)

0 Members and 1 Guest are viewing this topic.

starrybolt

  • Newbie
  • *
  • Posts: 6
    • View Profile
Hello.
I am currently developing a platformer game using SFML 2.0 and C++ (VS 2010)

The aim of this game is to collect all the keys within the time frame.
I have managed to get the sprite to collide with the keys however I am unable to be able to remove the sprite from the screen when the keys are collected. On suggestion from my lecturer I tried to make the sprite transparent, currently shown in code below, however once collision has happened between the sprites then the key reappears.
Does anyone have a solution in which I can remove the sprite completely?
Or even just setting it transparent after collision?

All help is appreciated.
Many thanks.
starrybolt.

Here is my code:

bool collSpritePos = false;

                //creating the texture
                sf::Texture keyTexture;
                //loading the file
                keyTexture.loadFromFile("keysprite.png");
                //creating a vector with an array of 7 sprites
                std::vector<sf::Sprite> keySprites(7, sf::Sprite(keyTexture));
                //for loop to check through array and set sprite positions
                for(int i = 0; i < keySprites.size(); i++)
                {
                        keySprites[0].setPosition(460, 80);
                        keySprites[1].setPosition(140, 275);
                        keySprites[2].setPosition(800, 300);
                        keySprites[3].setPosition(700, 500);
                        keySprites[4].setPosition(125, 500);
                        keySprites[5].setPosition(250, 850);
                        keySprites[6].setPosition(700, 800);
                }

                bool keySpriteVisible = true;

                //collision detection for key sprite with player sprite

                if(keySprites[6].getGlobalBounds().intersects(playerSprite.getGlobalBounds()))
                {
                        collSpritePos = true;
                        keySpriteVisible = false;
                        std::cout << "COLLISION" << std::endl;
                        if(!keyMusic.openFromFile("coinMusic.ogg"))
                        {
                                std::cout << " Error loading 'coinMusic' file." << std::endl;
                        }

                        keyMusic.play();

                        if(keySpriteVisible == false)
                        {
                                keySprites[6].setColor(sf::Color::Transparent);
                        }
                }
 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Simply don't draw it. This question has been answered many times already, have you searched a bit?

Your loop is pointless, by the way...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

starrybolt

  • Newbie
  • *
  • Posts: 6
    • View Profile
Hi.
Thankyou for the reply.
Yes I did alot of research before posting but everything I found and implemented did not work correctly and the key sprite was still showing after collision.
Okay thankyou.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
To add on to what Nexus said, here are some notes.

First of all, is all of that code happening inside of a game loop? I'm going to assume it is.

//creating the texture
sf::Texture keyTexture;
//loading the file
keyTexture.loadFromFile("keysprite.png");
//creating a vector with an array of 7 sprites
std::vector<sf::Sprite> keySprites(7, sf::Sprite(keyTexture));
//for loop to check through array and set sprite positions
 
Assuming this is happening in your game loop, you are reloading your file and recreating your sprites every frame. You should create them once before entering your game loop. This also applies to the keyMusic variable later on in your code.

//for loop to check through array and set sprite positions
        for(int i = 0; i < keySprites.size(); i++)
        {
            keySprites[0].setPosition(460, 80);
            keySprites[1].setPosition(140, 275);
            keySprites[2].setPosition(800, 300);
            keySprites[3].setPosition(700, 500);
            keySprites[4].setPosition(125, 500);
            keySprites[5].setPosition(250, 850);
            keySprites[6].setPosition(700, 800);
        }
 
Why are you setting the position of the sprites in a loop? This loop isn't needed since you are setting each individual sprite. Notice how you are looping with the variable "i", but then you never use "i" in the loop. You are setting all of your sprites to the same thing seven different times.

if(keySpriteVisible == false)
{
   keySprites[6].setColor(sf::Color::Transparent);
}
 
Again, assuming all of this code is happening inside of a game loop, this won't really do anything. You are setting the color of keySprite[6], but then in your next loop you will just recreate keySprite[6] so it will lose its transparency (refer back to my first comment above). This is why your current solution does not work.

If you create the sprite vector before your game loop, then perhaps a better solution to your problem would be to remove that element from the vector when you detect a collision. Then, later when you loop through your objects to draw them, the key will be missing from the vector so it will not get drawn. This is what Nexus is referring to.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Yes I did alot of research before posting
A forum search for "remove sprite" yields these threads, all of which are very closely related to your problem:
http://en.sfml-dev.org/forums/index.php?topic=12680
http://en.sfml-dev.org/forums/index.php?topic=14507
http://en.sfml-dev.org/forums/index.php?topic=3569

And read Arcade's post carefully, he gives very helpful advice.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

starrybolt

  • Newbie
  • *
  • Posts: 6
    • View Profile
Hi.

Yes it was within the game loop, Im sorry I forgot to mention that.
Thankyou Arcade and Nexus, I appreciate your help as this has been bothering me for a couple of days now.
I apologise for my stupid errors, I am rather new to SFML.

Kind regards,
starrybolt.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
No worries :)

Sometimes searching for a bit longer can also save yourself time, it would probably have been quicker than typing the whole post and waiting for answers.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Julius123

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
bool deleteSprite = false;

if (!deleteSprite) {
     window.draw(YOUR SPRITE);
}



all you got to do is set deleteSprite to true and your sprite will delete.