Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XNA My menuscreen类将不接受通过以前的spritebatch.begin绘制_C#_Xna_Spritebatch - Fatal编程技术网

C# XNA My menuscreen类将不接受通过以前的spritebatch.begin绘制

C# XNA My menuscreen类将不接受通过以前的spritebatch.begin绘制,c#,xna,spritebatch,C#,Xna,Spritebatch,在我的游戏状态管理系统中,我有一个屏幕管理器和一个菜单屏幕,它们都添加到了我当前的游戏中。屏幕管理器打开一个spritebatch,然后调用所有已订阅屏幕的draw方法,但当我在第一个已订阅屏幕中调用第一个spritebatch.draw时,我收到一个错误,指出必须在之前调用spritebatch.Begin,事实就是这样 ScreenManager绘图代码: public override void Draw(GameTime gameTime) { Sprit

在我的游戏状态管理系统中,我有一个屏幕管理器和一个菜单屏幕,它们都添加到了我当前的游戏中。屏幕管理器打开一个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);
    }
    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。

我发现最好在后续的Draw调用中传递spritebatch,而不是在类中传递spritebatch,以确保它们是相同的spritebatch,如果这可能是我以前的方式,那会很酷,但我现在已经解决了我自己的问题。我曾经有过这样的问题,并且选择了同样的解决方案,现在我对此很满意。如果你以另一种方式解决问题,请发布答案并让我们都知道。你应该将答案作为答案而不是评论发布。