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

Author Topic: Sprite can't be positioned  (Read 2836 times)

0 Members and 1 Guest are viewing this topic.

Lauro

  • Newbie
  • *
  • Posts: 10
    • View Profile
Sprite can't be positioned
« on: November 30, 2019, 10:48:05 am »
Hi everyone, i'm having an issue.
in the constructor of the class Starship i put this:
    sprite.setPosition((float)sf::VideoMode::getDesktopMode().width/2, (float)sf::VideoMode::getDesktopMode().height/2);

 
to have the starship sprite in the center of the screen whatever resolution the window is using.

The problem is that the sprite is always constructed in 0,0 even if i put random integers in the setPosition call.

What am i doing wrong?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11028
    • View Profile
    • development blog
    • Email
Re: Sprite can't be positioned
« Reply #1 on: November 30, 2019, 11:25:26 am »
The issue is probably somewhere else in your code.

Can you provide a complete and minimal example that reproduces the issue?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Sprite can't be positioned
« Reply #2 on: November 30, 2019, 08:10:54 pm »
why are you using desktop mode, instead of your sf::RenderWindow variable?

I would do it like
sprite.setPosition((float)render_window.getSize().x/2-sprite.getSize().x/2, (float)render_window.getSize().y/2-sprite.getSize().y/2)
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite can't be positioned
« Reply #3 on: December 01, 2019, 10:47:07 pm »
I would say that it's more reliable to use the view rather than the size of the window since the view is the actual co-ordinates used for the positioning.
And, since you want the centre and the view knows what its centre is, you can just get that:
sprite.setPosition(view.getCenter());
Or, if you don't have a separate view, you can get the view from the window:
sprite.setPosition(window.getView().getCenter());

Remember, though, that the top-left corner of the sprite will be placed in the centre of the window unless you alter the sprite's origin. You probably want it to be in its centre:
sprite.setOrigin(sf::Vector2f(sprite.getTexture().getSize() / 2u));
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything