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

Author Topic: Add center() method for Sprite  (Read 3661 times)

0 Members and 1 Guest are viewing this topic.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Add center() method for Sprite
« on: September 14, 2016, 10:17:24 pm »
Hello. Very often I need to center sprite relative to some position. So I have to do something like
sprite.setPosition(newX-sprite.getGlobalBounds().width/2, newY-sprite.getGlobalBounds().height/2)

but it will be very useful to do something this:
sprite.center(newX, newY);
 

Thanks.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Add center() method for Sprite
« Reply #1 on: September 14, 2016, 11:34:36 pm »
Probably best not to use global bounds to move a position; it will move it every time this is called.

What's wrong with using this:
sprite.setOrigin(sprite.getLocalBounds().width / 2.f, sprite.getLocalBounds().height / 2.f);
just once and then every time you set its position, it'll be centred.

If you use Plinth (I do all the time!), you can use its SFML Anchors. For example:
sprite.setOrigin(pl::Anchor::Local::getCenter(sprite));
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Add center() method for Sprite
« Reply #2 on: September 15, 2016, 06:54:59 am »
Thanks for reply, it helped.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Add center() method for Sprite
« Reply #3 on: September 16, 2016, 11:22:05 pm »
You are welcome. Glad you found it useful :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*