【问题标题】:Null Reference Exception in XNAXNA 中的空引用异常
【发布时间】:2012-01-15 18:54:51
【问题描述】:

我对 XNA 有疑问。我试图通过扩展和收缩来让文本“呼吸”,但它无法正常运行。它编译得很好,但我不断收到“NullReferenceException:对象引用未设置为对象的实例”。运行后出错。我有两个文件,Game1.cs 和 BreathingText.cs。

BreathingText.cs:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TestingThings
{
    class BreathingText
    {       
        public void drawText(string textToDraw, int X, int Y)
        {
            int fontRestraint = 1;
            Game1 game = new Game1();
            GraphicsDeviceManager graphics = game.graphics;
            SpriteBatch sB = game.spriteBatch;
            SpriteFont font = game.gameFont;
            Vector2 FontPos = new Vector2(X, Y);
            Vector2 StrCenter = new Vector2(0,0); //font.MeasureString(textToDraw) / 2;

             sB.Begin();
             sB.DrawString(font,
                           textToDraw,
                           FontPos,
                           Color.LightGreen,
                           0,
                           StrCenter,
                           1.0f,
                           SpriteEffects.None,
                           0.5f);
            if (fontRestraint == 1)
            {
                font.Spacing += 0.25F;
            }
            else if (fontRestraint == 0)
            {
                font.Spacing -= 0.25F;
            }
                if (font.Spacing == 17)
                {
                    fontRestraint = 0;
                }
                else if (font.Spacing == 0)
                {
                    fontRestraint = 1;
                }
            sB.End();
        }
    }
}

异常发生在sB.Begin()。我认为它正在发生,因为 Game1.cs 中的 spriteBatch 没有初始化,但在我看来它应该如此。这是 Game1.cs:

namespace TestingThings
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public GraphicsDeviceManager graphics;
        public SpriteBatch spriteBatch;
        public SpriteFont gameFont;

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

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            gameFont = Content.Load<SpriteFont>("Celestia");
        }

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

            base.Update(gameTime);
        }

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

            BreathingText breath = new BreathingText();
            breath.drawText("test", graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height - 25);
            base.Draw(gameTime);
        }
    }
}

我不确定它为什么不起作用,因为它似乎应该这样做。但是,我是新手,解决方案很可能就在我的眼皮底下。我在 Game1 的 Draw 方法中有 BreathingText.cs 代码,它运行良好,直到我将它移到它自己的类中。

任何帮助将不胜感激。

谢谢, 文选先生

【问题讨论】:

  • 您的代码或 XNA 代码中是否发生了空引用异常?
  • 它发生在我自己的 BreathingText.cs 中的sB.Begin(),所以我假设这是我自己做的。不过,就像我说的那样,我是 C# 新手,所以我仍在学习如何调试这样的东西。
  • 我不使用 XNA,但看起来Game1.LoadContent 没有在基类的构造函数中调用。什么时候应该调用它?
  • 我认为这意味着 sB 为空.. 你能放一个简单的 Debug.Assert(sB != null) (调试位于 System.Diagnostics 中),并告诉我是否崩溃是吗?
  • 我这样做了,是的,它崩溃了。这是错误消息:i.imgur.com/LTG1a.png

标签: c# xna nullreferenceexception


【解决方案1】:

您正在 Drawtext 方法 (Game1 game = new Game1();) 中创建 game1 类的新实例,因此永远不会为该游戏的 spritebatch 分配值(它将是 null)。

您应该在创建文本类时传入游戏对象的实例,然后在您的 drawtext 方法中使用它。

class BreathingText
{
    private Game1 _Game1; // Use this were you have 'game' currently

    public BreathingText(Game1 game)
    {
        _Game1 = game;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 2012-09-29
    • 2012-01-31
    • 2013-09-21
    • 2013-05-14
    相关资源
    最近更新 更多