C# XNA - 从底部中心开始绘制
C# XNA - Start draw from bottom-center
通常 XNA 从左上角开始绘制精灵,但我想从底部中心开始绘制对象,这怎么办?
假设您要在位置 X
xY
上绘制图像 Width
xHeight
。
spriteBatch.Draw(texture, position, Color.White);
首先让我们通过从位置的 Y 坐标减去图像高度来将图像的底部设置为这些坐标(减去因为在 XNA 中 Y 轴是倒置的,不像你的数学 class)
spriteBatch.Draw(texture, position + new Vector2(0, -texture.Height), Color.White);
其次,让我们通过从位置的 X 坐标减去图像宽度的一半来将图像设置为左侧。
spriteBatch.Draw(texture, position + new Vector2(-texture.Width/2, -texture.Height), Color.White);
给你了。
编辑: 另一个想法:您可以创建名为 DrawPosition
的新变量并在需要时使用该变量,而不是总是减去。那看起来像这样:
private Texture2D texture;
public Vector2 position;
public Vector2 DrawPosition
{ get { return position + new Vector2(-texture.Width/2, -texture.Height); } }
public void Draw(SpriteBatch spriteBatch)
{ spriteBatch.Draw(texture, DrawPosition, Color.White); }
或者,如果这个新变量对您没有意义,请创建一个函数来 return DrawPosition()
public Vector2 DrawPosition()
{ return position + new Vector2(-texture.Width/2, -texture.Height); }
您想在 SpriteBatch.Draw
调用中指定不同的来源。默认值为 0,0(左上角)。请注意,原点是相对于精灵的,而不是屏幕。
因此,如果您的 sprite 是 64x64,则您希望底部中心使用 32x64 的原点。
例如使用 this override (MSDN)
spriteBatch.Draw (
texture,
position,
sourceRectangle,
color,
rotation,
new Vector2(32, 64), // origin
scale,
effects,
layerDepth
)
如果你愿意,你可以即时计算这些。例如,如果您使用的是完整纹理,则可以将其指定为 new Vector2(texture.Center.X, texture.Height)
。或者,如果您使用的是 sprite sheet.
,则可以将其基于 sourceRectangle
您需要指定一堆其他参数才能使用这些 Draw
覆盖,但您可以只传递默认值。默认值是:
sourceRectangle: null = full texture
color: Color.White = default color (sprite colors will be used)
rotation: 0f = no rotation
scale: 1f = default scale
efects: SpriteEffects.None = no flipping
layerDepth: 0 = default layer
通常 XNA 从左上角开始绘制精灵,但我想从底部中心开始绘制对象,这怎么办?
假设您要在位置 X
xY
上绘制图像 Width
xHeight
。
spriteBatch.Draw(texture, position, Color.White);
首先让我们通过从位置的 Y 坐标减去图像高度来将图像的底部设置为这些坐标(减去因为在 XNA 中 Y 轴是倒置的,不像你的数学 class)
spriteBatch.Draw(texture, position + new Vector2(0, -texture.Height), Color.White);
其次,让我们通过从位置的 X 坐标减去图像宽度的一半来将图像设置为左侧。
spriteBatch.Draw(texture, position + new Vector2(-texture.Width/2, -texture.Height), Color.White);
给你了。
编辑: 另一个想法:您可以创建名为 DrawPosition
的新变量并在需要时使用该变量,而不是总是减去。那看起来像这样:
private Texture2D texture;
public Vector2 position;
public Vector2 DrawPosition
{ get { return position + new Vector2(-texture.Width/2, -texture.Height); } }
public void Draw(SpriteBatch spriteBatch)
{ spriteBatch.Draw(texture, DrawPosition, Color.White); }
或者,如果这个新变量对您没有意义,请创建一个函数来 return DrawPosition()
public Vector2 DrawPosition()
{ return position + new Vector2(-texture.Width/2, -texture.Height); }
您想在 SpriteBatch.Draw
调用中指定不同的来源。默认值为 0,0(左上角)。请注意,原点是相对于精灵的,而不是屏幕。
因此,如果您的 sprite 是 64x64,则您希望底部中心使用 32x64 的原点。
例如使用 this override (MSDN)
spriteBatch.Draw (
texture,
position,
sourceRectangle,
color,
rotation,
new Vector2(32, 64), // origin
scale,
effects,
layerDepth
)
如果你愿意,你可以即时计算这些。例如,如果您使用的是完整纹理,则可以将其指定为 new Vector2(texture.Center.X, texture.Height)
。或者,如果您使用的是 sprite sheet.
sourceRectangle
您需要指定一堆其他参数才能使用这些 Draw
覆盖,但您可以只传递默认值。默认值是:
sourceRectangle: null = full texture
color: Color.White = default color (sprite colors will be used)
rotation: 0f = no rotation
scale: 1f = default scale
efects: SpriteEffects.None = no flipping
layerDepth: 0 = default layer