SFML community forums

Help => Graphics => Topic started by: Tweezy on April 05, 2013, 07:42:39 pm

Title: std::vector with SFML 2.0 Sprite.
Post by: Tweezy on April 05, 2013, 07:42:39 pm
Hi all, I am using vectors to loop the amount of sprites that are rendered to my game screen.

//load the invaders images
        sf::Texture invaderTexture;
        invaderTexture.loadFromFile("images/invaders.png");
        std::vector<sf::Sprite> invaderSprites(10, sf::Sprite(invaderTexture));

        while (App.isOpen())// Process events
        {
                for (int i = 0; i < 5; i++)
                {
                        invaderSprites[i].setPosition((60*i) + 50, 10);
                }
                for (int i = 0; i < 5; i++)
                {
                        App.draw(invaderSprites[i]);
                }
                App.draw(back);
                App.display();
               
        }

This works perfectly, and I also found out that I was stupidly rendering the invaders and then the screen in past "issues". Now, SFML 2.0 classes aren't working with this code if they are out of the for loop:

Error: class "std::vector<sf::sprite, std::allocator<sf::Sprite>>" has no member "setPosition"

Putting everything in my for loop will just, diminish certain working parts for my game. I've tried doing

sf::Sprite<SpriteNameHere> but that wont rectify my issue. Any ideas here? Will this be one of the issues where I have to change everything?
Title: Re: std::vector with SFML 2.0 Sprite.
Post by: Nexus on April 05, 2013, 07:52:38 pm
Show the code that does not work.
Title: Re: std::vector with SFML 2.0 Sprite.
Post by: Tweezy on April 05, 2013, 08:01:01 pm
That always comes up if I use invaderSprites out of the for loop, which I do a hell of a lot of times, without showing all of my code I don't see how it helps.

invaderSprites.setPosition(10,10);

I know it has got something to do with having it inside a vector, as if I just used a normal sf::Sprite invaderSprite(invaderTexture); then the loop no longer works so I have to use this vector.
Title: Re: std::vector with SFML 2.0 Sprite.
Post by: G. on April 05, 2013, 09:09:03 pm
invaderSprites is a vector.
invaderSprites[i] is a sprite.

It's like food (sprites) inside a fridge (vector), and you're trying to eat the fridge.  :D
Title: Re: std::vector with SFML 2.0 Sprite.
Post by: Tweezy on April 05, 2013, 09:11:11 pm
std::vector<sf::Sprite>invaderSprites(10, sf::Sprite(invaderTexture));

I was thinking it may of been something to do with the vector, but sf::Sprite is still in there so I kind of went away from that sort of thinking
Title: Re: std::vector with SFML 2.0 Sprite.
Post by: G. on April 05, 2013, 09:29:48 pm
Do you know C++ or programming? Using an array or a vector is basic (and important), your lessons (or tons of resources on the internet) probably explain it better than I could.

invaderSprites.setPosition(10,10); calls setPosition on the vector, and vector doesn't have a setPosition method.
invaderSprites[i].setPosition(10,10); calls setPosition on the element which is at  the i index in your vector.

If you want to call setPosition on a particular sprite, you have to access it by its index.
Title: Re: std::vector with SFML 2.0 Sprite.
Post by: FRex on April 05, 2013, 09:32:45 pm
In plain post text [ i ] turns into italics..
That's so funny in here..
Too bad you edited it out..
Title: Re: std::vector with SFML 2.0 Sprite.
Post by: Tweezy on April 05, 2013, 09:38:28 pm
Do you know C++ or programming? Using an array or a vector is basic (and important), your lessons (or tons of resources on the internet) probably explain it better than I could.

invaderSprites.setPosition(10,10); calls setPosition on the vector, and vector doesn't have a setPosition method.
invaderSprites[i].setPosition(10,10); calls setPosition on the element which is at  the i index in your vector.

If you want to call setPosition on a particular sprite, you have to access it by its index.

I'm learning c++ but I've only had one lecture on vectors. I guess my original assumption of needing to edit my layout is correct?

I know about arrays too.

In plain post text [ i ] turns into italics..
That's so funny in here..
Too bad you edited it out..

What?
Title: Re: std::vector with SFML 2.0 Sprite.
Post by: FRex on April 05, 2013, 09:53:07 pm
Quote
What?
.G made funny error that he removed before you seen it. Don't mind it.
Title: Re: std::vector with SFML 2.0 Sprite.
Post by: Tweezy on April 05, 2013, 09:57:34 pm
Quote
What?
.G made funny error that he removed before you seen it. Don't mind it.

Okay. Could you possibly help with this?
Title: Re: std::vector with SFML 2.0 Sprite.
Post by: FRex on April 05, 2013, 10:15:55 pm
To do something with all elements of the vector you have to loop through it, there is no other way.
Title: Re: std::vector with SFML 2.0 Sprite.
Post by: Tweezy on April 05, 2013, 10:35:11 pm
To do something with all elements of the vector you have to loop through it, there is no other way.

Thought so.

Thanks you all for making my mind up lol