我怎样才能缩小我的大纹理,使其保持居中?

How can I shrink my large texture so that it stays centered?

当我缩小大纹理时,它并没有缩小图像。它只是从右下角开始沿对角线向上切割图像。

这是我绘制精灵的方式:

spriteBatch.Draw(Tiles[0].TileTexture, new Vector2(x * TileWidth, y * TileHeight), Color.White);

这是我正在测试的基本图像。

这是 16,16 时的样子

和64,64

如果您使用 16x16 图像,则单元格大小应为 16x16。但是如果你想在 16x16 单元格中绘制 64x64 图像,你应该将图像缩小到 16x16,但在这种情况下,64x64 和 16x16 源图像之间没有区别。或者将单元格设置为 64x64 像素。

关于缩放,您只能使用整数比例,例如 2x、3x 等。不允许非整数比例以避免图像失真。

SpriteBatch.Draw 有 overloads。 要根据需要缩放图像,您可以使用重载 1、2 或 3,并将目标矩形作为第二个参数:

SpriteBatch.Draw(
    tileTexture,
    new Rectange(x * TileWidth, y * TileHeight, TileWidth, TileHeight),
    Color.White);

源图像将被拉伸成矩形。

或者您可以使用最后两个重载中的一个来缩放图像

SpriteBatch.Draw(
    tileTexture,
    new Vector2(x * TileWidth * scaleX, y * TileHeight * scaleY),
    null,
    Color.White,
    0,
    Vector2.Zero,
    new Vector2(scaleX, scaleY),
    SpriteEffects.None,
    0);

另外,如果你只需要用一个相同的图块填充背景,你可以用任何 wrap SemplerState 参数调用 spritebatch begin methos(PointWrap 对我来说更好)并像这样绘制一次图块: SpriteBatch.Draw( 瓷砖纹理, 新矩形 (0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);