【问题标题】:Smoothly Sprite Rotation平滑精灵旋转
【发布时间】:2015-03-21 18:10:13
【问题描述】:

我正在使用 C# 和 XNA 编写游戏。我正在尝试平滑精灵旋转。这是我的代码:

 protected void RotateSpriteTo(int rotateTo)
    {

        if (rotateGradus > 360)
            rotateGradus = 0;

            if (rotateTo > rotateGradus)
            {
                while (rotateTo > rotateGradus)
                {
                    rotateGradus += 1;
                    rotate = (rotateGradus * (float)Math.PI) / 180;
                }
            }
             else
            {
                while (rotateTo < rotateGradus)
                    rotateGradus -=1;
                rotate = (rotateGradus * (float)Math.PI) / 180;
            }
    }

以及我如何调用这个方法,例如:

        if (Keyboard.GetState().IsKeyDown(Keys.A))
        {
            squarePosition.X -= speed;
            RotateSpriteTo(180);
        }

但是精灵会立即旋转。我该如何解决?

【问题讨论】:

    标签: c# rotation xna sprite


    【解决方案1】:

    不要立即旋转 180 度,而是尝试在 3 秒内执行此操作(又名。

    RotateSpriteTo((180/3) * gameTime.ElapsedGameTime);
    

    此外,您可能希望使用 MathHelper.ToRadians 而不是 '(float)Math.PI) / 180'。并不是说它不起作用,但您不妨使用内置功能,既然它就在那里。

    【讨论】:

    • 为此,您需要将方法签名更改为void RotateSpriteTo(float rotateTo)
    • 哦,真的,我的错。 RotateAmount 会增加一个值,或者存储一个本地值,该值将由我的答案中的参数递增。
    • 同意。虽然我仍然喜欢你正确使用gameTime.ElapsedGameTime 作为标量。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多