XNA中的空引用异常

时间:2012-01-15 18:54:52

标签: c# xna nullreferenceexception

我遇到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代码并且它工作正常,直到我将它移动到它自己的类。

任何帮助都将不胜感激。

谢谢, MrAnthology

1 个答案:

答案 0 :(得分:2)

您正在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;
    }