简体   繁体   English

如何在 c# 中翻转精灵

[英]how to flip a sprite in c#

I'm making a little game based on XNA game development's tutorial game "shooter"... after a bit of trial and error, I made my own animated sprite that i can move around the screen.我正在根据 XNA 游戏开发的教程游戏“射手”制作一个小游戏……经过反复试验,我制作了自己的动画精灵,我可以在屏幕上移动。 The problem is, he's always facing right.问题是,他总是面对正确的。

How can i change this (image below) so that he faces the right direction every time the key event is pressed?!我怎样才能改变这个(下图),以便每次按下按键事件时他都面向正确的方向?!

I'm pretty new to C# as you might have guessed, and I realize this may be more complicated than i thought, so i just need to know how to make the sprite face right when moving right and left when he's moving left.正如您可能已经猜到的那样,我对 C# 很陌生,我意识到这可能比我想象的要复杂,所以我只需要知道如何让精灵在他向左移动时向右和向左移动。

image图片

Thank you in advance.先感谢您。

You can also do this by passing the SpriteEffects.FlipHorizontally option to your SpriteBatch.Draw() method.您也可以通过将SpriteEffects.FlipHorizontally选项传递给您的SpriteBatch.Draw()方法来执行此操作。 But as others have said this will have more overhead than using a sprite sheet.但正如其他人所说,这将比使用精灵表有更多的开销。

Usually a sprite sheet will contain images for each direction.通常,精灵表将包含每个方向的图像。 You can flip the image at runtime, but it adds image processing that is unnecessary.您可以在运行时翻转图像,但它会添加不必要的图像处理。 I would suggest to you that you simply create a sprite sheet up front with each animation baked in and just figure out which frame to display at runtime.我建议您只需在每个 animation 中预先创建一个精灵表,然后确定在运行时显示哪个帧。

See an example of a simple sprite sheet here 在此处查看简单精灵表的示例

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.这将左右“翻转”纹理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM