1

我正在尝试使用 XNA 和 C# 为 windows 制作一个简单的 3D 游戏。正如每个教程甚至 MSDN 所建议的那样,我试图创建视图矩阵,但我收到了 NullReferenceException 错误。

错误内容为:对象引用未设置为对象的实例。它指向投影矩阵的定义:

projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
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 Series3D1
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        Effect effect;

        VertexPositionColor[] vertices;

        Matrix viewMatrix;
        Matrix projectionMatrix;

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        GraphicsDevice device;

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

        protected override void Initialize()
        {
            graphics.PreferredBackBufferWidth = 500;
            graphics.PreferredBackBufferHeight = 500;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();

            Window.Title = "Riemer's XNA Tutorials -- Series 1";

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            SetUpCamera();

            effect = Content.Load<Effect>("effects");

            SetUpVerticies();

            device = graphics.GraphicsDevice;
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

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

            effect.CurrentTechnique = effect.Techniques["ColoredNoShading"];

            effect.Parameters["xView"].SetValue(viewMatrix);
            effect.Parameters["xProjection"].SetValue(projectionMatrix);
            effect.Parameters["xWorld"].SetValue(Matrix.Identity);

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
            }

            device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1, VertexPositionColor.VertexDeclaration);

            base.Draw(gameTime);
        }

        private void SetUpVerticies()
        {
            vertices = new VertexPositionColor[3];

            vertices[0].Position = new Vector3(0f, 0f, 0f);
            vertices[0].Color = Color.Red;
            vertices[1].Position = new Vector3(10f, 10f, 0f);
            vertices[1].Color = Color.Green;
            vertices[2].Position = new Vector3(10f, 0f, -5f);
            vertices[2].Color = Color.Yellow;
        }

        private void SetUpCamera()
        {
            viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 50), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);
        }
    }
}
4

2 回答 2

5

您需要device = graphics.GraphicsDevice在调用之前设置您的SetUpCamera()。该SetUpCamera方法要求该device字段已经被分配,它没有按照您当前的顺序。

于 2012-05-12T03:27:10.303 回答
2
protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);

    SetUpCamera();

    effect = Content.Load<Effect>("effects");

    SetUpVerticies();

    device = graphics.GraphicsDevice;
}

必须

protected override void LoadContent()
{
    device = graphics.GraphicsDevice;

    spriteBatch = new SpriteBatch(GraphicsDevice);

    SetUpCamera();

    effect = Content.Load<Effect>("effects");

    SetUpVerticies();
}

这样device就设置好了(这是 和 所要求的SetUpCameraSetUpVerticies

调试提示:检查当地人以验证您的假设,您会看到deviceis null. 您也可以通过将变量悬停在您的行中来做到这一点...

于 2012-05-12T03:28:24.633 回答