SFML community forums

Help => General => Topic started by: MarcusM on October 18, 2013, 11:49:40 am

Title: [SOLVED] Flipping the texture on an AnimatedSprite class
Post by: MarcusM 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.
Title: Re: Flipping the texture on an AnimatedSprite class
Post by: Ixrec on October 18, 2013, 11:57:30 am
Try passing a negative number to setScale().
Title: Re: Flipping the texture on an AnimatedSprite class
Post by: MarcusM 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?
Title: Re: Flipping the texture on an AnimatedSprite class
Post by: Ixrec 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.
Title: Re: Flipping the texture on an AnimatedSprite class
Post by: MarcusM on October 18, 2013, 12:37:56 pm
Ah cheers! Thanks for the explanation.