C# 在屏幕中间放置矩形(XNA SpRITE)

C# 在屏幕中间放置矩形(XNA SpRITE),c#,xna,C#,Xna,所以我试着做一个小游戏只是为了好玩,但我很快就撞到墙上了。我想要playerGray框,从屏幕的正中央开始。但是,在我当前的代码中,它是从盒子的角点开始居中的,而不是它的中心。在它从盒子中心开始居中之前,我需要改变什么 /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param

所以我试着做一个小游戏只是为了好玩,但我很快就撞到墙上了。我想要playerGray框,从屏幕的正中央开始。但是,在我当前的代码中,它是从盒子的角点开始居中的,而不是它的中心。在它从盒子中心开始居中之前,我需要改变什么

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            Viewport viewport = GraphicsDevice.Viewport;
            Vector2 viewportmid = new Vector2(viewport.Width / 2, viewport.Height / 2);
            Vector2     origin = new Vector2(Player.Width / 2, Player.Height / 2);
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            spriteBatch.Draw(Player, viewportmid, Color.White, origin);
            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

提前谢谢!=新的网站,所以不能直接发布图像。这里有一个链接:

您需要将viewportmid偏移您的精灵大小的一半,才能顺利工作。从互联网上尝试了很多其他的东西,这个简单的答案就解决了所有问题。谢谢