1

我找不到为什么它不起作用。先谢谢了。抱歉,代码量很大,我不确定错误在哪里。问题是它只显示黑屏和加载鼠标。它不显示加载屏幕或菜单。

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    SpriteBatch mBatch;
    Texture2D mTheQuantumBros2;
    MenuComponent menuComponent;
    public static Rectangle screen;
    public static string GameState = "Menu";
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferMultiSampling = false;
        graphics.IsFullScreen = true;
        graphics.PreferredBackBufferWidth = 1366;
        graphics.PreferredBackBufferHeight = 768;
        graphics.ApplyChanges();
    }

    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        mBatch = new SpriteBatch(this.graphics.GraphicsDevice);
        ContentManager aLoader = new ContentManager(this.Services);
        aLoader.RootDirectory = "Content";
        mTheQuantumBros2 = aLoader.Load<Texture2D>("TheQuantumBros2") as Texture2D;
        menuComponent.LoadContent(Content);
    }

    protected override void UnloadContent()
    {

    }

    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        switch (GameState)
        {
            case "Menu":
                menuComponent.Update(gameTime);
            break;
        }

        base.Update(gameTime);
    }

    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        spriteBatch.Begin();
        switch (GameState)
        {
            case "Menu":
                menuComponent.Draw(spriteBatch);
                break;
        }
        base.Draw(gameTime);
    }
}

}

其他类:

class MenuComponent
{
    KeyboardState keyboard;
    KeyboardState prevKeyboard;

    SpriteBatch mBatch;
    Texture2D mTheQuantumBros2;

    GameTime gameTime;

    MouseState mouse;
    MouseState prevMouse;

    SpriteFont spriteFont;

    List<string> buttonList = new List<string>();

    int selected = 0;

    public MenuComponent()
    {
        buttonList.Add("Campaign");
        buttonList.Add("Multiplayer");
        buttonList.Add("Zombies");
        buttonList.Add("Quit Game");

    }

    public void LoadContent(ContentManager Content)
    {
        spriteFont = Content.Load<SpriteFont>("Font");
    }

    public void Update(GameTime gameTime)
    {
        keyboard = Keyboard.GetState();
        mouse = Mouse.GetState();

        if (CheckKeyboard(Keys.Up))
        {
            if (selected > 0) selected--;
        }

        if (CheckKeyboard(Keys.Down))
        {
            if (selected < buttonList.Count - 1) selected++;
        }

        prevMouse = mouse;
        prevKeyboard = keyboard;
    }

    public bool CheckMouse()
    {
        return (mouse.LeftButton == ButtonState.Pressed && prevMouse.LeftButton == ButtonState.Released);
    }

    public bool CheckKeyboard(Keys key)
    {
        return (keyboard.IsKeyDown(key) && prevKeyboard.IsKeyDown(key));
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        Color color;
        int linePadding = 3;

        spriteBatch.Begin();
        mBatch.Begin();
        mBatch.Draw(mTheQuantumBros2, new Rectangle(300, 150, mTheQuantumBros2.Width, mTheQuantumBros2.Height), Color.White);
        if (gameTime.TotalGameTime.TotalSeconds <= 3)
        {
            mBatch.End();
            for (int i = 0; i < buttonList.Count; i++)
            {
                color = (i == selected) ? Color.LawnGreen : Color.Gold;
                spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((Game1.screen.Width / 2) - (spriteFont.MeasureString(buttonList[i]).X / 2), (Game1.screen.Height / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
            }
            spriteBatch.End();
        }
    }
}

}

4

2 回答 2

2

该声明的目的是什么:(gameTime.TotalGameTime.TotalSeconds <= 3)? 此外,您永远不会在菜单组件中更新游戏时间。因此,上述代码会出现意外行为。

您还在调用 end 之前调用 spriteBatch.Begin() 两次......所以这应该给你一个错误。检查那些问题。

编辑:既然你只在不到 3 秒的时间里调用 spriteBatch.End() ,那之后就再也不会调用它了?我认为。

作为一个潜在的修复......我会尝试只在 game1.cs 中调用 spriteBatch.Begin() 和 spriteBatch.End()。所以:

//In your menu class
public void Draw(SpriteBatch spriteBatch)
{
    Color color;
    int linePadding = 3;


    if (gameTime.TotalGameTime.TotalSeconds <= 3)
    { 
        mBatch.Begin();
        mBatch.Draw(mTheQuantumBros2, new Rectangle(300, 150, mTheQuantumBros2.Width, mTheQuantumBros2.Height), Color.White);
        mBatch.End(); 
    }
    else
    {

        for (int i = 0; i < buttonList.Count; i++)
        {
            color = (i == selected) ? Color.LawnGreen : Color.Gold;
            spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((Game1.screen.Width / 2) - (spriteFont.MeasureString(buttonList[i]).X / 2), (Game1.screen.Height / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
        }
    }
}

回到 game1.cs,在调用 base.Draw() 之前调用 spriteBatch.End()。

一般来说,最好只使用一个 spriteBatch,我相信它比开始和结束两个不同的批次要快。

编辑2:

只是嗯...复制并粘贴我猜。在我进行调整后它工作正常(您可以在底部阅读它们)。

你重构了代码:

游戏1.cs:

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    MenuComponent menuComponent;

    public static Rectangle screen;
    public static string GameState = "Menu";
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferMultiSampling = false;
        //graphics.IsFullScreen = true;
        graphics.PreferredBackBufferWidth = 1366;
        graphics.PreferredBackBufferHeight = 768;
        graphics.ApplyChanges();
    }

    protected override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {

        spriteBatch = new SpriteBatch(GraphicsDevice);
        menuComponent = new MenuComponent();
        menuComponent.LoadContent(Content, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
    }


    protected override void UnloadContent()
    {

    }

    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        switch (GameState)
        {
            case "Menu":
                menuComponent.Update(gameTime);
            break;
        }

        base.Update(gameTime);
    }

    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        spriteBatch.Begin();
        switch (GameState)
        {
            case "Menu":
                menuComponent.Draw(spriteBatch, gameTime);
                break;
        }
        spriteBatch.End();
        base.Draw(gameTime);

    }

菜单组件:

    KeyboardState keyboard;
    KeyboardState prevKeyboard;

    MouseState mouse;
    MouseState prevMouse;

    GraphicsDevice graphicsDevice;
    Texture2D mTheQuantumBros2;

    SpriteFont spriteFont;

    List<string> buttonList = new List<string>();

    int selected = 0;
    int screenWidth;
    int screenHeight;
    public MenuComponent()
    {
        buttonList.Add("Campaign");
        buttonList.Add("Multiplayer");
        buttonList.Add("Zombies");
        buttonList.Add("Quit Game");

    }

    public void LoadContent(ContentManager Content, int _screenWidth, int _screenHeight)
    {
        spriteFont = Content.Load<SpriteFont>("Font");
        mTheQuantumBros2 = Content.Load<Texture2D>("TheQuantumBros2");

        screenHeight = _screenHeight;
        screenWidth = _screenWidth;
    }

    public void Update(GameTime gameTime)
    {
        keyboard = Keyboard.GetState();
        mouse = Mouse.GetState();

        if (CheckKeyboard(Keys.Up))
        {
            if (selected > 0) selected--;
        }

        if (CheckKeyboard(Keys.Down))
        {
            if (selected < buttonList.Count - 1) selected++;
        }

        prevMouse = mouse;
        prevKeyboard = keyboard;
    }

    public bool CheckMouse()
    {
        return (mouse.LeftButton == ButtonState.Pressed && prevMouse.LeftButton == ButtonState.Released);
    }

    public bool CheckKeyboard(Keys key)
    {
        return (keyboard.IsKeyDown(key) && prevKeyboard.IsKeyDown(key));
    }

    public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
    {
        Color color;
        int linePadding = 3;

        if (gameTime.TotalGameTime.TotalSeconds <= 3)
        {
            spriteBatch.Draw(mTheQuantumBros2, new Rectangle(300, 150, mTheQuantumBros2.Width, mTheQuantumBros2.Height), Color.White);
        }
        else
        {
            for (int i = 0; i < buttonList.Count; i++)
            {
                color = (i == selected) ? Color.LawnGreen : Color.Gold;
                spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((screenWidth / 2) - (spriteFont.MeasureString(buttonList[i]).X / 2), (screenHeight / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
            }
        }
    }

有一堆小问题。例如,您在每次更新调用时将 menuComp = 设置为一个新的菜单组件,因此其中的所有旧变量都会丢失,因此不会加载图像。SpriteBatch.Begin() 被连续调用两次,然后 end 被连续调用两次。应该使用一个 spriteBatch(最好来自 game1.cs)来绘制。通过该方法发送该 spritebatch,您无需再次调用 begin,出于性能原因,最好不要这样做。我注意到的最后一件事(我能想到的)是图形设备没有被设置为任何东西,因为您一直在创建它的新版本。哦,还有,你调用 graphicsDevice.Clear() 太早了,每次绘制只应该调用一次(在开始时,旧的图形信息会从屏幕上删除)。

于 2013-05-12T19:08:47.660 回答
1

我调试了您的程序/代码,但存在一些问题:

  • 有一个 MenuComponent menuComponent 的声明,但您没有在 Game1() 构造函数中创建实例。所以添加:menuComponent = new MenuComponent()

  • 有 SpriteBatch mBatch 的声明,但您没有在 MenuComponent() 构造函数中创建实例。所以创建一个。GameTime gameTime 变量也是如此。

目前我没有 SpriteFont 和 TheQuantumBros2 纹理。您可以上传您的项目并分享吗?

于 2013-05-12T19:26:48.010 回答