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

Author Topic: std::vector with SFML 2.0 Sprite.  (Read 6302 times)

0 Members and 1 Guest are viewing this topic.

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
std::vector with SFML 2.0 Sprite.
« 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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: std::vector with SFML 2.0 Sprite.
« Reply #1 on: April 05, 2013, 07:52:38 pm »
Show the code that does not work.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: std::vector with SFML 2.0 Sprite.
« Reply #2 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.
« Last Edit: April 05, 2013, 08:03:57 pm by Tweezy »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: std::vector with SFML 2.0 Sprite.
« Reply #3 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

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: std::vector with SFML 2.0 Sprite.
« Reply #4 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

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: std::vector with SFML 2.0 Sprite.
« Reply #5 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.
« Last Edit: April 05, 2013, 09:32:21 pm by G. »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: std::vector with SFML 2.0 Sprite.
« Reply #6 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..
Back to C++ gamedev with SFML in May 2023

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: std::vector with SFML 2.0 Sprite.
« Reply #7 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?
« Last Edit: April 05, 2013, 09:57:05 pm by Tweezy »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: std::vector with SFML 2.0 Sprite.
« Reply #8 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.
Back to C++ gamedev with SFML in May 2023

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: std::vector with SFML 2.0 Sprite.
« Reply #9 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?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: std::vector with SFML 2.0 Sprite.
« Reply #10 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.
Back to C++ gamedev with SFML in May 2023

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: std::vector with SFML 2.0 Sprite.
« Reply #11 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

 

anything