XNA - 找不到错误

时间:2013-05-12 18:54:39

标签: c# xna

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

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();
        }
    }
}

}

2 个答案:

答案 0 :(得分:2)

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

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

编辑:因为你只调用了spriteBatch.End(),如果通过不到3秒,那么之后永远不会再调用它?我认为。

作为一个潜在的修复...我会尝试只在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:

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

你是重构的代码:

game1.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);

    }

MenuComponent中:

    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()方法,这只应该每次调用一次(在开始时,从屏幕上删除旧的图形信息)。

答案 1 :(得分:1)

我调试了你的程序/代码,但有一些问题:

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

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

目前我还没有SpriteFont和TheQuantumBros2纹理。您是否可以上传项目并共享它?