如何在XNA游戏工作室中旋转Texture2D?

时间:2012-04-10 16:14:00

标签: c# xna

嗨我必须旋转Texture2D,但我不知道为什么。 它就像一张照片,我需要将它旋转180°,因为它位于头部,应该是正确的。

问题是,我需要在使用spriteBatch.Draw之前这样做,这可能吗?

2 个答案:

答案 0 :(得分:3)

Draw方法有overload,可让您旋转纹理。我不确定我理解为什么你需要在绘制之前旋转纹理。缩放和旋转通常在绘图期间完成。正如乔尔指出的那样,旋转是使用弧度完成的,你还需要注意原点,即你旋转的点。

答案 1 :(得分:3)

纹理将旋转180度。

    protected override void Draw(GameTime gameTime)
    {
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap, DepthStencilState.None, RasterizerState.CullCounterClockwise, null);
        spriteBatch.Draw(texture, Position, null, Color.White, (float)Math.PI, new Vector2(texture.Width, texture.Height), 1, SpriteEffects.None, 0);
        spriteBatch.End();
        base.Draw(gameTime);
    }

您也可以使用SpriteEffects垂直翻转纹理:

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap, DepthStencilState.None, RasterizerState.CullCounterClockwise, null);
        spriteBatch.Draw(texture, Position, null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.FlipVertically, 0);
        spriteBatch.End();
        base.Draw(gameTime);
    }
相关问题