How to flip a sprite in C #

I am making a small game based on the XNA shooter game development ... after some trial and error, I created my own animated sprite that I can move around the screen. The problem is that he is always always right.

How can I change this (image below) so that it looks in the right direction every time a key event is clicked ?!

I am new to C #, as you might have guessed, and I understand that it can be harder than I thought, so I just need to know how to make the sprite face correct when moving left and right, when it moves left.

picture

Thanks in advance.

+4
source share
3 answers

You can also do this by passing the SpriteEffects.FlipHorizontally parameter SpriteEffects.FlipHorizontally your SpriteBatch.Draw() method. But, as others have said, this will have more overhead than using a sprite sheet.

+5
source

Typically, a sprite sheet will contain images for each direction. You can flip the image at runtime, but it adds image processing that is not needed. I would like to suggest that you simply create a sprite sheet in front with each processed animation and simply determine which frame is displayed at runtime.

Here is an example of a simple sprite sheet

+2
source
 SpriteEffects s = SpriteEffects.FlipHorizontally; int smX = 200; //smx is the 'x' coordinates. public void runChar() { if (Keyboard.GetState().IsKeyDown(Keys.Left)) { smX -= 2; s = SpriteEffects.FlipHorizontally; //oposite direction. } else if (Keyboard.GetState().IsKeyDown(Keys.Right)) { smX += 2; s = SpriteEffects.None; //original direction. } } spriteBatch.Draw(sM, new Rectangle(smX, 200, 100, 100), null, Color.White, rotation, new Vector2(50, 50), s, 0f); 

This will flip the texture left and right.

0
source

All Articles