2013-05-09 69 views
0

你如何居中图像的矩形内有:如何将图像置于矩形中?

Batch.draw(imagename, new Rectangle(x, y, imagename.width, imagename.height), Color.White); 
+0

我知道我已经回答了这一点,但请你的问题提供更多的上下文。 – gunr2171 2013-05-09 23:32:50

回答

0

其中一个重载SpriteBatch.Draw()要求一个Texture2D(图像)的,一个Vector2(在图像的位置),以及Color(中色调的图像)。您不应该使用矩形绘制图像。只需使用一些数学来获得该位置的正确的XY值。

您将不得不考虑图像的高度/宽度以及包含对象的高度/宽度(如屏幕或其他)。

另一种选择是使用采用Origin参数的过载。将其设置为相对图像的中心。这会将图像的“绘制原点”从左上角更改为中心。

1

您可以使用原点重载spritebatch

Batch.draw(imagename, new Rectangle(x, y, imagename.width, imagename.height), Color.White,0f,new Vector2(imagename.Width /2, imagename.Height /2) ,SpriteEffects.Null, 0);