【发布时间】:2015-05-06 23:54:46
【问题描述】:
我正在用 C# 和 XNA 4.0 制作游戏。它使用多个对象(例如玩家角色、敌人、平台等),每个对象都有自己的纹理和碰撞箱。使用类似于以下的代码创建和绘制对象:
class Object
{
Texture2D m_texture;
Rectangle m_hitbox;
public Object(Texture2D texture, Vector2 position)
{
m_texture = texture;
m_hitbox = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, m_hitbox, Color.White);
}
}
一切正常,但我也想让玩家调整游戏窗口的大小。主游戏类使用以下代码来执行此操作:
private void Update(GameTime gameTime)
{
if (playerChangedWindowSize == true)
{
graphics.PreferredBackBufferHeight = newHeight;
graphics.PreferredBackBufferWidth = newWidth;
graphics.ApplyChanges();
}
}
这将不可避免地导致无论何时更改窗口大小,对象的位置和命中框都会变得不准确。有没有一种简单的方法可以根据新的窗口大小更改位置和命中框?如果新窗口的宽度是以前的两倍,我可能只是将每个对象的碰撞框的宽度加倍,但我敢肯定这是一种糟糕的做法。
【问题讨论】: