【问题标题】:Moving Integers, NullReference error [duplicate]移动整数,NullReference 错误 [重复]
【发布时间】:2013-12-04 14:40:00
【问题描述】:

我有三个类,主类称为Game1.cs,另外两个类位于文件夹中,分别称为Images.cs 和Resize.cs。在Images 中,图像已加载并可以使用。在Resize 中,我想绘制一个名为size 的整数,它等于1。我得到一个空值错误,不知道为什么。

如果我尝试在Game1.cs 中放置任何大小,则会出现相同的错误。

Game1.cs:

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 **
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public static SpriteFont font1;

        private StartUp.Resize resize;

        static new Rectangle con = new Rectangle(330, 246, 364, 84);
        static new Rectangle cre = new Rectangle(330, 430, 364, 84);
        static new Rectangle exit = new Rectangle(330, 522, 364, 84);
        static new Rectangle logo = new Rectangle(216, 37, 591, 71);
        static new Rectangle opt = new Rectangle(330, 338, 364, 84);
        static new Rectangle play = new Rectangle(330, 154, 364, 84);
        static new Rectangle ver = new Rectangle(7, 701, 215, 39);

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

            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;
            graphics.ApplyChanges();

            StartUp.Images.Content = Content;
        }

        protected override void Initialize()
        {
            this.IsMouseVisible = true;
            base.Initialize();
        }

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

            StartUp.Images.Load();
            font1 = Content.Load<SpriteFont>(@"Fonts\Font1");
        }

        protected override void UnloadContent()
        {
        }

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

            resize.size = 1;

            base.Update(gameTime);
        }

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

            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise);
            spriteBatch.Draw(StartUp.Images.Continue, con, Color.White);
            spriteBatch.Draw(StartUp.Images.Credits, cre, Color.White);
            spriteBatch.Draw(StartUp.Images.Exit, exit, Color.White);
            spriteBatch.Draw(StartUp.Images.Play, play, Color.White);
            spriteBatch.Draw(StartUp.Images.Options, opt, Color.White);
            spriteBatch.Draw(StartUp.Images.Logo, logo, Color.White);
            spriteBatch.Draw(StartUp.Images.Version, ver, Color.White);
//error here 
            spriteBatch.DrawString(font1, resize.size.ToString(), new Vector2(0, 0), Color.White);
//error here
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

Resize.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace **.StartUp
{
    public class Resize
    {
        public int size = 1;

        public int continueWidth;
        public int continueHeight;

        public int ContinueWidth;
        public int ContinueHeight;
    }
}

【问题讨论】:

    标签: c# class xna int draw


    【解决方案1】:

    那是因为你在使用它之前没有初始化它。

    在你的 Game1 构造函数中初始化它:

    public Game1()
    {
        ...
    
        resize = new Resize();
    }
    

    【讨论】:

    • 天哪,我错过了,谢谢你的帮助。
    • @RyanFoy 不客气!祝你好运。
    猜你喜欢
    • 2013-12-15
    • 2015-05-19
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    相关资源
    最近更新 更多