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);
}
}
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.