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

Author Topic: sf::Sprite question?  (Read 2017 times)

0 Members and 2 Guests are viewing this topic.

aegir

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
sf::Sprite question?
« on: June 24, 2015, 10:59:31 pm »
I'm not sure what to google for this problem so it might already be answered, my apologies in advance.

Im drawing lots of tiles, so i draw floor tiles under player/monster (to avoid black square when i move). First floor sprite then on the same position player/monster sprite.
It all works except player sprite disappears when i move onto original enemy position but not original player position like so:
http://imgur.com/a/wnGwV

code for setting sprites is:
            temp.setTexture(holder["grass"]);
                        temp.setPosition(i.second);
                        sprites.push_back(std::make_unique<sf::Sprite>(temp));

                        temp.setTexture(holder["monster"]);
                        temp.setPosition(i.second);
                        sprites.push_back(std::make_unique<sf::Sprite>(temp));
 
Code is exactly the same when drawing player.

I have no idea how to approach fixing that so help :D


« Last Edit: June 24, 2015, 11:02:10 pm by aegir »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Sprite question?
« Reply #1 on: June 24, 2015, 11:07:40 pm »
Have you tried to use the debugger? Such problems can usually be found rather quickly :)

For us, who don't know the code, it's very difficult to help without a complete/minimal example. And it also takes much longer...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

aegir

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: sf::Sprite question?
« Reply #2 on: June 24, 2015, 11:52:32 pm »
Thanks for reply here is as minimal example as i managed to get it, in attachment are required files and cpp. To reproduce just move to position of first npc to the left.

(click to show/hide)

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: sf::Sprite question?
« Reply #3 on: June 25, 2015, 12:17:11 am »
for (unsigned int i = 0; i < sprites.size(); ++i)
   window.draw((*sprites[i]));
 

The draw order is being determined by the order you put sprites into your vector. The last sprite in your vector will be the last one drawn, so If two sprites are at the same location, the last one drawn will be the one on top. I haven't looked at your code very closely, but what is likely happening is that you are drawing your player and its grass tile before you are drawing the monster and its grass tile. Therefore, the monster's grass tile will be in front of your player because it will be farther back in your vector.

The solution would be to draw all of your background grass tiles before drawing anything else.

aegir

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: sf::Sprite question?
« Reply #4 on: June 25, 2015, 12:24:22 am »
Thank you :D I will attempt to do that tomorrow.

 

anything