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

Author Topic: [SOLVED] Flipping the texture on an AnimatedSprite class  (Read 2010 times)

0 Members and 1 Guest are viewing this topic.

MarcusM

  • Newbie
  • *
  • Posts: 14
    • View Profile
[SOLVED] Flipping the texture on an AnimatedSprite class
« on: October 18, 2013, 11:49:40 am »
Hi guys.

I am currently implementing spritesheets into my game rather than having static images moving around all over the places.
For this, I am using the source provided on the Github wiki: https://github.com/SFML/SFML/wiki/Source%3A-AnimatedSprite

It's working just fine, except I am having some trouble flipping the sprite when the character is moving in the opposite direction of the spritesheet.

I have a little trouble wrapping my head around how it should be done, as the AnimatedSprite class isn't inheriting from sf::Sprite, so that I can't do the simple:
// flip X
sprite.setTextureRect(sf::IntRect(width, 0, -width, height));

Has anyone implemented a such feature in their AnimatedSprite class? If so, how did you do it?

Thanks for your time.
« Last Edit: October 18, 2013, 12:38:21 pm by MarcusM »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Flipping the texture on an AnimatedSprite class
« Reply #1 on: October 18, 2013, 11:57:30 am »
Try passing a negative number to setScale().

MarcusM

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Flipping the texture on an AnimatedSprite class
« Reply #2 on: October 18, 2013, 12:10:07 pm »
Try passing a negative number to setScale().

Tried implementing a AnimatedSprite::flipTexture() function:
void AnimatedSprite::flipTexture()
{
    setOrigin(getGlobalBounds().width/2, 0);
    setScale(-1.f,1.f);
    setOrigin(0, 0);
}

But I am having some trouble with the sprite changing position. Am I using the setOrigin correctly?

EDIT: This appears to work:
void AnimatedSprite::flipTexture()
{
    setOrigin(getGlobalBounds().width, 0);
    setScale(-1.f,1.f);
}

Will I face any problems with having a modified origin?
« Last Edit: October 18, 2013, 12:12:25 pm by MarcusM »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Flipping the texture on an AnimatedSprite class
« Reply #3 on: October 18, 2013, 12:16:53 pm »
The origin only affects how the various transformation functions behave, so the answer to that question is entirely within your own code.  There's nothing special about the default origin.

Edit: If you were wondering why multiple setOrigins didn't work, that's because properties like rotation, scaling, positioning, etc are not actually used until the draw() call.  If you change the origin, rotation, position, or scale twice without an intervening draw(), then the first change has no effect on anything.
« Last Edit: October 18, 2013, 12:24:36 pm by Ixrec »

MarcusM

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Flipping the texture on an AnimatedSprite class
« Reply #4 on: October 18, 2013, 12:37:56 pm »
Ah cheers! Thanks for the explanation.