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

Author Topic: Setting sprite but getting error in RenderWindow::Draw  (Read 1327 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Setting sprite but getting error in RenderWindow::Draw
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Setting sprite but getting error in RenderWindow::Draw
« Reply #1 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)
Laurent Gomila - SFML developer

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Setting sprite but getting error in RenderWindow::Draw
« Reply #2 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
« Last Edit: June 22, 2014, 11:05:32 pm by Canvas »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything