我正在尝试开发一个JRPG/地牢爬虫作为夏季项目,我正在自动取款机清理我的代码。在战斗中,它存在“健康条”,一个健康条由每个角色需要绘制的3个矩形组成(根据上下文,总共2-8个字符意味着一次最多24个健康条矩形)。
我绘制健康条的旧方法是基于1个纹理,我从1个像素中提取一个像素,然后在一个矩形区域上绘制。基本上我使用了"HealtBar“纹理作为调色板…例如,如果源矩形是(0,0,1,1),它表示绘制一个黑色矩形。
Texture2D mHealthBar;
mHealthBar = theContentManager.Load<Texture2D>("HealthBar");
public override void Draw(SpriteBatch theSpriteBatch)
{
//draw healtbar
theSpriteBatch.Draw(mHealthBar, new Rectangle((int)Position.X+5,
(int)(Position.Y - 20), (MaxHealth * 5)+7, 15),
new Rectangle(0, 0, 1, 1), Color.Black);
theSpriteBatch.Draw(mHealthBar, new Rectangle((int)(Position.X + 8),
(int)(Position.Y - 17), ((MaxHealth * 5)), (mHealthBar.Height) - 2),
new Rectangle(2, 2, 1, 1), Color.White);
theSpriteBatch.Draw(mHealthBar, new Rectangle((int)(Position.X + 8),
(int)(Position.Y - 17), ((Health * 5)), (mHealthBar.Height) - 2),
new Rectangle(3, 2, 1, 1), Color.Red);
}这不是一个好看的解决方案。因此,我尝试创建一个抽象类,以减少代码量并提高代码的清晰度。
abstract class Drawer
{
static private Texture2D _empty_texture;
static public void DrawRect(SpriteBatch batch, Color color, Rectangle rect)
{
_empty_texture = new Texture2D(batch.GraphicsDevice, 1, 1);
_empty_texture.SetData(new[] { Color.White });
batch.Draw(_empty_texture, rect, color);
}
}有了这个新的抽屉类,我就可以去掉"mHealthBar“变量,以一种更美观的代码方式来绘制矩形。但是当我开始使用新的方式来绘制游戏开始出现帧率问题的时候,原来的方式是smooth...what是导致帧率问题的原因吗?
Drawer.DrawRect(theSpriteBatch, Color.Black, new Rectangle((int)Position.X+5,
(int)(Position.Y - 20), (MaxHealth * 5)+7, 15));
Drawer.DrawRect(theSpriteBatch, Color.White, new Rectangle((int)(Position.X + 8),
(int)(Position.Y - 17), (MaxHealth * 5), 9));
Drawer.DrawRect(theSpriteBatch, Color.Red, new Rectangle((int)(Position.X + 8),
(int)(Position.Y - 17), (Health * 5), 9));发布于 2011-08-02 17:22:12
问题是你每次想要绘制的时候都会创建一个新的纹理并设置颜色。
你应该做的是只创建一次纹理(当加载其他内容时)。
https://stackoverflow.com/questions/6909790
复制相似问题