【发布时间】:2016-08-21 12:48:01
【问题描述】:
我希望能够缩放我的精灵并将它们保持在与缩放之前相同的位置。我使用精灵的中心作为原点参数,因为我也希望能够旋转我的精灵。
我确定解决方案将是微不足道的,但我找不到解决此问题的适当/通用解决方案。
如果不是很清楚我做了一些图片来展示我的代码,这段代码的结果以及我想要实现的目标。
1 - 这是我的代码和结果,这是我缩放精灵时得到的结果,您可以看到精灵已缩放但它“移动”了:
正如评论中所建议的,这里是代码:
Vector2 scale = Vector2.One;
float rotation = 0;
public void Update(GameTime gameTime)
{
if (Input.IsKeyPressed(Keys.P))
scale += new Vector2(0.05f, 0.0f);
if (Input.IsKeyPressed(Keys.M))
scale -= new Vector2(0.05f, 0.0f);
if (Input.IsKeyPressed(Keys.O))
scale += new Vector2(0.0f, 0.05f);
if (Input.IsKeyPressed(Keys.L))
scale -= new Vector2(0.0f, 0.05f);
if (Input.IsKeyPressed(Keys.D))
rotation += MathHelper.ToRadians(5);
if (Input.IsKeyPressed(Keys.Q))
rotation -= MathHelper.ToRadians(5);
Input.Update(gameTime);
}
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, Matrix.CreateScale(4));
spriteBatch.Draw(Sprites.BACKGROUND, Vector2.Zero, new Rectangle(0, 0, 128, 128), Color.White);
Vector2 marioPosition = new Vector2(64, 112 - 32);
Rectangle source = new Rectangle(0,0,16,32);
Vector2 origin = source.Size.ToVector2() / 2;
spriteBatch.Draw(Sprites.MARIO, marioPosition + origin, source, Color.White, rotation, origin, scale, SpriteEffects.None, 0f);
spriteBatch.End();
}
2 - 这就是我想要实现的,我知道我可以通过移动我的原点来做到这一点,但我想将它保持在精灵的中心,这样我就可以围绕这个点进行旋转:http://pasteboard.co/rzMfc0p.png
【问题讨论】:
-
欢迎来到 Stackoverflow 。您能否将代码粘贴到问题的正文中,这样可以更轻松地阅读您的问题而无需在页面之间移动,并且如果您链接到的页面被删除,我们仍然为未来的读者提供您问题中的代码.