我正在尝试根据屏幕高度调整图像大小,我遇到了麻烦。目前的问题是图像根本没有显示。这是代码:
class Board
{
private Texture2D texture;
private int screenWidth = Game1.Instance.GraphicsDevice.Viewport.Width;
private int screenHeight = Game1.Instance.GraphicsDevice.Viewport.Height;
private Vector2 location;
private Rectangle destination;
public Board(Texture2D texture)
{
this.texture = texture;
this.location = new Vector2(200, 0);
this.destination = new Rectangle((int)location.X, (int)location.Y, texture.Width * (screenHeight / texture.Height), screenHeight);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
spriteBatch.Draw(texture,
destination,
Color.White);
spriteBatch.End();
}
}
之前显示的图像虽然仍然太宽,但我知道主循环中的代码很好。简而言之,我的问题是......这段代码有什么问题,还有更好的方法吗?
答案 0 :(得分:1)
texture.Width * (screenHeight / texture.Height)
使用整数除法。如果纹理大于屏幕,它将返回0.宽度为0时,您将看不到纹理。相反,将一个操作数强制转换为double
或float
:
texture.Width * (screenHeight / (double)texture.Height)
将返回double
,允许除法/乘法按预期工作。