SFML community forums

Help => General => Topic started by: Canvas on June 22, 2014, 10:50:37 pm

Title: Setting sprite but getting error in RenderWindow::Draw
Post by: Canvas on June 22, 2014, 10:50:37 pm
Hello there people,

I'm having a little problem with populating a sprite, but when attempting to draw the sprite I get a n error in the RenderWindow Draw function.

Here is the code that I am using to pull back my character object to draw

std::vector<CharacterObject*> State::GetCharacters()
{
        std::vector<CharacterObject*> characters;

        for (auto character : Characters)
        {
                characters.push_back(&character);
        }

        return characters;
}
 

Inspecting &character the variable you can see the spriteSheet is set up, just to make it clear the spriteSheet  is a sheet of sprites, so basically the sprite is is setup at the moment to just draw the spriteSheet, here is the sprite setup code

void CharacterObject::Setup()
{
        sprite.setTexture(spriteSheet);
}
 

But in the image the sprite seems to not be set, is this due to me passing a pointer around or am I setting the sprite wrong?

Here is an image below.
Title: Re: Setting sprite but getting error in RenderWindow::Draw
Post by: Laurent on June 22, 2014, 10:56:21 pm
You're pushing the address of destroyed objects to your vector.

Solution: use references in your loop to avoid creating temporary copies.

for (auto& character : Characters)
Title: Re: Setting sprite but getting error in RenderWindow::Draw
Post by: Canvas on June 22, 2014, 10:59:07 pm
Ok I have just updated that and it works, however the sprite is just a while box, any reason why? Do you need more code to inspect this?

Its ok I fixed it, it was due to texture being set, then the texture being used later on, i have fixed this. cheers for the help Laurent
Title: Re: Setting sprite but getting error in RenderWindow::Draw
Post by: Nexus on June 22, 2014, 11:06:25 pm
Please read the tutorials.
http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php#the-white-square-problem