在我的游戏状态管理系统中,我有一个屏幕管理器和一个菜单屏幕,两者都添加到我当前的游戏中。屏幕管理器打开一个spritebatch然后调用所有订阅屏幕的draw方法,但是当我在我的第一个订阅屏幕上看到第一个SpriteBatch.Draw调用时,我得到一个错误,说先前必须调用SpriteBatch.Begin,它是。
ScreenManager绘制代码:
public override void Draw(GameTime gameTime)
{
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
for (int i = 0; i < ScreenList.Count; i++)
{
if (ScreenList[i].Visible)
ScreenList[i].Draw(gameTime);
}
SpriteBatch.End();
base.Draw(gameTime);
}
和我的MenuScreen绘图代码:
public override void Draw(GameTime gameTime)
{
SpriteBatch.Draw(Background, new Rectangle(0, 0, ScreenManager.Game.Window.ClientBounds.Width, ScreenManager.Game.Window.ClientBounds.Height),
null, Color.White, 0f, new Vector2(Background.Width / 2, Background.Height / 2), SpriteEffects.None, 1f);
SpriteBatch.DrawString(Font, Text, Position, Color, 0f, Origin, Scale, SpriteEffects.None, 0f);
foreach (MenuEntry entry in EntryList)
{
entry.Draw(gameTime);
}
}
最后我的基础游戏初始化了我创建所有这些组件的代码:
protected override void Initialize()
{
screenManager = new ScreenManager(this, Content.Load<SpriteFont>("Fonts/DefaultFont"), Content.Load<Texture2D>("Images/Background"));
Components.Add(screenManager);
screenManager.AddScreen(new MainMenuScreen(screenManager, "Tanks", new Vector2(screenManager.Game.Window.ClientBounds.Width / 2, 100)));
base.Initialize();
}
注意:MainMenuScreen继承自MenuScreen,这是一个抽象类。我的MenuScreen类也从ScreenManager类传递了一个spritebatch,因此它们应该是相同的spriteBatch。