4
\$\begingroup\$

In Microsoft.Xna.Framework.Graphics.SpriteEffects there is the possibility to flip a texture horizontally or vertically before rendering. But can one do both at the same time?

When I use: spriteEffect = SpriteEffects.FlipHorizontally | SpriteEffects.FlipVertically; with spriteEffect being a parameter of the particle. The method spriteBatch.Draw(spriteSheet, currentPositionForDraw, sourceRectangle, currentColorMultiplied, currentRotation, currentOriginForDraw, Temp.delta, spriteEffect, 0); does not draw the particle flipped on both axis.

\$\endgroup\$

3 Answers 3

6
\$\begingroup\$

Yes. If you look at the definition of that enum you'll see it has [FlagsAttribute] which means you can combine the enum values with the | operator.

\$\endgroup\$
4
  • \$\begingroup\$ Sorry, but how do I do that ? \$\endgroup\$
    – ares_games
    Jan 13, 2013 at 18:35
  • \$\begingroup\$ SpriteEffects.FlipHorizontally | SpriteEffects.Flipvertically \$\endgroup\$
    – Adam
    Jan 13, 2013 at 18:43
  • \$\begingroup\$ I edited my question. \$\endgroup\$
    – ares_games
    Jan 13, 2013 at 18:50
  • 2
    \$\begingroup\$ @pad You edited the question to suggest that this answer is insufficient, but it's still marked as correct. Which is it? \$\endgroup\$
    – Anko
    Jan 14, 2013 at 4:27
7
\$\begingroup\$

If you don't care about rotation, you can simply set the origin of the sprite to its center and rotate it 180 degrees (Math.PI). This is the same as flipping horizontally and vertically.

If you do care about rotation, and the sprite is flipped both ways, you can just add Math.PI to the angle and you should be done (eg. 45 degrees + 180 degrees rotation looks the same as 45-degrees flipped on both axes).

\$\endgroup\$
0
0
\$\begingroup\$

Just in case someone is still having trouble, this is how I do it:

bool flipHorizontal;
bool flipVertical;

SpriteEffects flipX = flipHorizontal ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
SpriteEffects flipY = flipVertical ? SpriteEffects.FlipVertically : SpriteEffects.None;

spriteBatch.Draw( ... , flipX | flipY, ... )

flipHorizontal and flipVertical could be expressions matching against an enum, or some other bit of logic.

The pipe operator | is actually a bitwise OR that can be applied to enums with the [Flags] attribute, so if you want to get fancy, look into that.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .