c# - 带有 C# 的 XNA 中的 NullReferenceException

标签 c# xna 3d

我正在尝试使用 XNA 和 C# 为 Windows 制作一个简单的 3D 游戏。我试图按照每个教程甚至 MSDN 的建议创建 View 矩阵,但我收到了 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);
        }
    }
}

最佳答案

在调用 SetUpCamera() 之前,您需要设置您的 device = graphics.GraphicsDeviceSetUpCamera 方法要求 device 字段已经分配,​​但您当前的订单中没有。

关于c# - 带有 C# 的 XNA 中的 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10560800/

相关文章:

c# - 使用图形 API 查询 Azure AD

c# - 为提取的方法添加单元测试

c# - 自动更新属性

c# - 从 Windows Phone 7 设备静默发送电子邮件?

c# - 将 XNA 3.1 转换为 XNA 4.0。 StorageContainer.TileLocation

math - 如何计算两个 3d 圆上的一对最近点?

3d - ar.js 3d 模型不显示

c# - 在 Python 中使用 System.Collections.Generic.List`1[System.Byte]

c# - MonoGame 中的 ContentLoadException

java - 计算机图形运算矩阵