XNA 4.0游戏开发实例初学者指南

时间:2013-10-14 20:55:02

标签: c# nullreferenceexception xna-4.0

我正在阅读 XNA 4.0游戏开发示例:初学者指南这本书,我不断收到Null Reference Exception错误。特别是在我的代码的game.Tick部分。只是想知道我编码不正确或者Visual Studio是否只是表现得很奇怪。 感谢

namespace Level_Editor
{
    public partial class MapEditor : Form
    {
        public Game1 game;


        public MapEditor()
        {
            InitializeComponent();
        }

        private void LoadImageList()
        {
            string filepath = Application.StartupPath +
                @"\Content\Textures\PlatformTiles.png";
            Bitmap tileSheet = new Bitmap(filepath);
            int tilecount = 0;
            for (int y = 0; y < tileSheet.Height / TileMap.TileHeight; y++)
            {
                for (int x = 0; x < tileSheet.Width / TileMap.TileWidth; x++)
                {
                    Bitmap newBitmap = tileSheet.Clone(new
                        System.Drawing.Rectangle(
                            x * TileMap.TileWidth,
                            y * TileMap.TileHeight,
                            TileMap.TileWidth,
                            TileMap.TileHeight),
                            System.Drawing.Imaging.PixelFormat.DontCare);

                    imgListTiles.Images.Add(newBitmap);
                    string itemName = "";
                    if (tilecount == 0)
                    {
                        itemName = "Empty";
                    }
                    if (tilecount == 1)
                    {
                        itemName = "White";
                    }
                    listTiles.Items.Add(new
                        ListViewItem(itemName, tilecount++));
                }
            }

            FixScrollBarScales();
        }

        private void FixScrollBarScales()
        {
            Camera.ViewPortWidth = pctSurface.Width;
            Camera.ViewPortHeight = pctSurface.Height;

            Camera.Move(Vector2.Zero);

            vScrollBar1.Minimum = 0;
            vScrollBar1.Maximum =
                Camera.WorldRectangle.Height -
                Camera.ViewPortHeight;

            hScrollBar1.Minimum = 0;
            hScrollBar1.Maximum =
                Camera.WorldRectangle.Width -
                Camera.ViewPortWidth;
        }

        private void loadMapToolStripMenuItem_Click(
            object sender,
            EventArgs e)
        {
            try
            {
                TileMap.LoadMap(new FileStream(
                    Application.StartupPath + @"\MAP" +
                    cboMapNumber.Items[cboMapNumber.SelectedIndex] + ".MAP",
                    FileMode.Open));
            }
            catch
            {
                System.Diagnostics.Debug.Print("Unable to load map file");
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
           game.Exit();
            Application.Exit();
        }

        private void MapEditor_Load(object sender, EventArgs e)
        {
            LoadImageList();

            cboCodeValues.Items.Clear();
            cboCodeValues.Items.Add("Gemstone");
            cboCodeValues.Items.Add("Enemy");
            cboCodeValues.Items.Add("Lethal");
            cboCodeValues.Items.Add("EnemyBlocking");
            cboCodeValues.Items.Add("Start");
            cboCodeValues.Items.Add("Clear");
            cboCodeValues.Items.Add("Custom");

            for (int x = 0; x < 100; x++)
            {
                cboMapNumber.Items.Add(x.ToString().PadLeft(3, '0'));
            }

            cboMapNumber.SelectedIndex = 0;

            TileMap.EditorMode = true;

            backgroundToolStripMenuItem.Checked = true;
        }

        private void timerGameUpdate_Tick(object sender, EventArgs e)
        {
            if (hScrollBar1.Maximum < 0)
            {
                FixScrollBarScales();
            }

            game.Tick();

            if (game== null && game.HoverCodeValue != lblCurrentCode.Text)
               lblCurrentCode.Text =game.HoverCodeValue;

        }

        private void MapEditor_FormClosed(
            object sender,
            FormClosedEventArgs e)
        {
           game.Exit();
            Application.Exit();
        }

    }
}

2 个答案:

答案 0 :(得分:0)

通过查看您的代码,您似乎还没有初始化“游戏”对象。我知道Game类对象是在你的主类和程序的起点中定义的,但似乎你没有在MapEditor类中传递对它的引用。

这里,程序认为你已经定义了一个新的Game对象,但是还没有初始化它。这就是为什么它会吐出一个Null Reference Exception。您需要在此类中引用原始游戏对象,方法是传递它,或者通过使用全局变量。

答案 1 :(得分:0)

MapEditor构造函数中,您需要初始化Game1对象,但我建议您使用Game代替Game1,因为Game1是运行游戏的主要类,不应该以这种方式使用它。

尝试这样的事情:

public Game game;

public MapEditor(Game game)
{
    this.game = game;

    InitializeComponent();
}
相关问题