C#ArgumentNullException未处理(SpriteBatch.Draw)

时间:2016-04-20 20:14:06

标签: c# xna spritebatch

我实际上是XNA的新生,发现这个图书馆非常有趣,但我仍然缺乏一些知识,因为我觉得我无法自行解决这个问题:(

spriteBatch.Draw()方法说我的纹理是null,但是我已经将它加载到Resources.cs类中并在MainMenu.cs中传递了纹理,所以我真的不知道问题所在的位置,如果有人的话可以帮助我,我会非常感激!

Resources.cs

    class Resources
{
    public static Texture2D pixel;
    public static Texture2D startButton, loadButton, quitButton;

    public static SpriteFont consoleFont;

    public static void LoadContent(ContentManager Content)
    {
        pixel = Content.Load<Texture2D>("Pixel");
        consoleFont = Content.Load<SpriteFont>("Console");

        // UI Ressources :
        startButton = Content.Load<Texture2D>("UI/StartButton");
        loadButton = Content.Load<Texture2D>("UI/LoadButton");
        quitButton = Content.Load<Texture2D>("UI/QuitButton");
    }
}

MainMenu.cs

    class MainMenu
{
    // Fields
    List<Button> buttons = new List<Button>();

    // Constructors
    public MainMenu()
    {
        this.buttons.Add(new Button(new Vector2(480, 132), 256, 48, Resources.startButton));
        this.buttons.Add(new Button(new Vector2(480, 212), 256, 48, Resources.loadButton));
        this.buttons.Add(new Button(new Vector2(480, 292), 256, 48, Resources.quitButton));
    }

    // Methods

    // Update
    public void Update()
    {
    }

    // Draw
    public void Draw(SpriteBatch spriteBatch)
    {
        foreach (Button button in buttons)
        {
            button.Draw(spriteBatch);
        }
    }
}

Button.cs

    class Button : UIElement
{
    int width, height;
    Texture2D texture;

    public Button()
    {
    }

    public Button(Vector2 b_position, int b_width, int b_height, Texture2D b_texture)
    {
        this.position = b_position;
        this.width = b_width;
        this.height = b_height;
        this.texture = b_texture;
    }

    public void Update()
    {
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, position, Color.White);
    }
}

3 个答案:

答案 0 :(得分:0)

虽然我没有看到你的Game类,但我的第一个假设是你从未在Resources类中调用public static void LoadContent(ContentManager Content)方法。由于此函数正在实例化您的纹理,因此可能不会调用它。

答案 1 :(得分:0)

感谢您的回复! 确实我忘了包含Game类,这是调用资源的正确方法吗?

    public class GameMain : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    MainMenu mainMenu;

    public GameMain()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        this.IsMouseVisible = true;

        mainMenu = new MainMenu();
    }

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

        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here
        Resources.LoadContent(Content);
    }

    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        spriteBatch.Begin();
        mainMenu.Draw(spriteBatch);
        spriteBatch.End();
        base.Draw(gameTime);
    }
}

答案 2 :(得分:0)

简单的真实 您只需要知道Game1的函数的调用顺序。首先,调用Game1构造函数。那就是你放mainMenu = new MainMenu();的地方。之后转到InitializeLoad等等。您必须在mainMenu = new MainMenu();之后将Load从Game1构造函数移动到Resources.LoadContent(Content);函数,以便在MainMenu中使用资源之前加载资源。您的Game1.cs代码应如下所示:

public class GameMain : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    MainMenu mainMenu;

    public GameMain()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        this.IsMouseVisible = true;
    }

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

        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here
        Resources.LoadContent(Content);
        mainMenu = new MainMenu();
    }

    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        spriteBatch.Begin();
        mainMenu.Draw(spriteBatch);
        spriteBatch.End();
        base.Draw(gameTime);
    }
}