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

Author Topic: Problem with stars  (Read 2532 times)

0 Members and 1 Guest are viewing this topic.

Sebox

  • Newbie
  • *
  • Posts: 24
    • View Profile
Problem with stars
« on: August 04, 2014, 09:34:01 pm »
Hello everyone!

Now I make my platform game and now i dont know how to make a stars which we can collect.
I know how check if we will collect(intersetcs) but I dont know how to use texture to make more stars, for example now i loading gwiazda.png to memory

sf::Texture gwiazda;
sf::Sprite spritegwiazda;

gwiazda.loadFromFile("gwiazda.png");
spritegwiazda.setTexture(gwiazda);
spritegwiazda.setPosition(730,1200);

and now i make one star but how to make 300 or more and if i catch the star how i can delete this from map ??
I hope you understand what i mean.

Sorry for my English.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem with stars
« Reply #1 on: August 04, 2014, 09:46:10 pm »
Just make more sprites and give them the same texture.  Nothing special is required to make that work.

If you aren't sure how to manage 300 or more objects in general, you probably need to read a good book about C++, since some kind of STL container is going to be the right way of doing that, and everyone needs to learn the STL.

Deleting it will probably be as simple as removing it from whatever container you use.

Sebox

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Problem with stars
« Reply #2 on: August 05, 2014, 09:10:08 am »
I think that vector won't work good because player can collect the stars from different sides. Maybe i will have to make all star individually but there is no better way ?

janszy

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Problem with stars
« Reply #3 on: August 05, 2014, 10:50:13 am »
It doesn't matter which side the player collects the star, you still can use vectors.
std::vector<sf::Sprite> stars;
//create the stars
// ...

//put this code in your game loop
for(auto &thisStar: stars) //assuming you use C++11
{
    if(intersect(player, thisStar)) //check that player collected a star or not. Use your check function here.
    {
        stars.erase(thisStar); //If it did then remove the star's sprite from the vector and don't render it next time.
    }
}

 

Sebox

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Problem with stars
« Reply #4 on: August 05, 2014, 01:23:41 pm »
std::vector<sf::Sprite> stars;

stars.push_back(spritegwiazda);
stars.push_back(spritegwiazda1);
stars.push_back(spritegwiazda2);

vector<sf::Sprite>::iterator it;
       
for( size_t i = 0; i < stars.size(); ++i )
{
        oknoaplikacji.draw(stars[i]);
}
if(spritesheet.getGlobalBounds().intersects(spritegwiazda.getGlobalBounds()))
{
                stars.erase(stars.begin()+1);
}
and now "stars.push_back(spritegwiazda);" doesnt work because "spritegwiazda" is undefined so i dont can setPosition on the map.



janszy

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Problem with stars
« Reply #5 on: August 05, 2014, 02:06:48 pm »
You still need to declare your variables, load the texture, set the sprites etc. like in your first post. You don't have to create new variables for every star instance though. You can do it in a loop as well:
sf::Texture gwiazda;
gwiazda.loadFromFile("gwiazda.png"); // load the texture

int numStars = 5; //how many stars you want
std::vector<sf::Sprite> stars(numStars);

for(int i=0; i < numStars; ++i)
{
    sf::Sprite spritegwiazda;
    spritegwiazda.setTexture(gwiazda); // use the loaded texture
    spritegwiazda.setPosition(150,i*100); // you can set the position based on the value of i or generate a random position etc.
    stars.push_back(spritegwiazda);
}

kimci86

  • Full Member
  • ***
  • Posts: 123
    • View Profile
Re: Problem with stars
« Reply #6 on: August 05, 2014, 03:43:27 pm »
@janszy
Something is wrong in your code: you are using the fill constructor of std::vector and also push_back sprites.
This line
std::vector<sf::Sprite> stars(numStars);
creates a vector of numStars sprites, so there is no need to push back the sprites. Access sprites with stars[index].

By the way, the fill constructor can initialize the sprites with a copy of a given sprite.

What your code could be:
sf::Texture gwiazda;
gwiazda.loadFromFile("gwiazda.png");

int numStars = 5;
std::vector<sf::Sprite> stars(numStars, sf::Sprite(gwiazda));

for(int i=0; i < numStars; ++i)
    stars[i].setPosition(150,i*100);

janszy

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Problem with stars
« Reply #7 on: August 05, 2014, 05:42:29 pm »
Quote
Something is wrong in your code: you are using the fill constructor of std::vector and also push_back sprites.

You're right. I indeed mixed those. Thanks for the correction  ;)

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Problem with stars
« Reply #8 on: August 05, 2014, 07:25:17 pm »
I just wanted to toss an idea at you. Instead of constantly removing and adding stars, look into the concept of object pooling. Can save you a lot of trouble come performance time.

 

anything