空引用异常

发布于 2024-08-28 15:03:16 字数 6442 浏览 4 评论 0原文

“你调用的对象是空的。”

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

namespace XNAdev
{
    class Sprite
    {
        //The size of the Sprite
        public Rectangle Size;

        //Used to size the Sprite up or down from the original image
        public float Scale = 1.0f;

        //The current position of the Sprite
        public Vector2 Position = new Vector2(115, 0);
        //The texture object used when drawing the sprite
        private Texture2D mSpriteTexture;

        //Load the texture for the sprite using the Content Pipeline
        public void LoadContent(ContentManager theContentManager, string theAssetName)
        {
            mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
            Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
        }

        //Draw the sprite to the screen
        public void Draw(SpriteBatch theSpriteBatch)
        {
            theSpriteBatch.Draw(mSpriteTexture, Position,
                new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), Color.White,
                0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);

        }    
    }
}

我对 C# 很陌生,所以任何帮助都会很棒。

我不知道我的错误是什么。


namespace XNAdev
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Sprite mSprite;
        Sprite mSpriteTwo;
        Sprite mBackgroundOne;
        Sprite mBackgroundTwo;
        Sprite mBackgroundThree;
        Sprite mBackgroundFour;
        Sprite mBackgroundFive;





        public Game1()
        {           

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

        protected override void Initialize()
        {
            mSprite = new Sprite();
            mSpriteTwo = new Sprite();

            mBackgroundOne = new Sprite();
            mBackgroundOne.Scale = 2.0f;

            mBackgroundTwo = new Sprite();
            mBackgroundTwo.Scale = 2.0f;

            mBackgroundThree = new Sprite();
            mBackgroundThree.Scale = 2.0f;

            mBackgroundFour = new Sprite();
            mBackgroundFour.Scale = 2.0f;

            mBackgroundFive = new Sprite();
            mBackgroundFive.Scale = 2.0f;

            base.Initialize();
        }

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

            mSprite.Position = new Vector2(125, 245);

            mSpriteTwo.LoadContent(this.Content, "SquareGuy");
            mSpriteTwo.Position.X = 300;
            mSpriteTwo.Position.Y = 300;

            mBackgroundOne.LoadContent(this.Content, "Background01");
            mBackgroundOne.Position = new Vector2(0, 0);            

            mBackgroundTwo.LoadContent(this.Content, "Background02");
            mBackgroundTwo.Position = new Vector2(mBackgroundOne.Position.X + mBackgroundOne.Size.Width, 0);

            mBackgroundThree.LoadContent(this.Content, "Background03");
            mBackgroundThree.Position = new Vector2(mBackgroundTwo.Position.X + mBackgroundTwo.Size.Width, 0);

            mBackgroundFour.LoadContent(this.Content, "Background04");
            mBackgroundFour.Position = new Vector2(mBackgroundThree.Position.X + mBackgroundThree.Size.Width, 0);

            mBackgroundFive.LoadContent(this.Content, "Background05");
            mBackgroundFive.Position = new Vector2(mBackgroundFour.Position.X + mBackgroundFour.Size.Width, 0);            
        }

        protected override void UnloadContent()
        {

        }


        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            if (mBackgroundOne.Position.X < -mBackgroundOne.Size.Width)
            {
                mBackgroundOne.Position.X = mBackgroundFive.Position.X + mBackgroundFive.Size.Width;
            }

            if (mBackgroundTwo.Position.X < -mBackgroundTwo.Size.Width)
            {
                mBackgroundTwo.Position.X = mBackgroundOne.Position.X + mBackgroundOne.Size.Width;
            }

            if (mBackgroundThree.Position.X < -mBackgroundThree.Size.Width)
            {
                mBackgroundThree.Position.X = mBackgroundTwo.Position.X + mBackgroundTwo.Size.Width;
            }

            if (mBackgroundFour.Position.X < -mBackgroundFour.Size.Width)
            {
                mBackgroundFour.Position.X = mBackgroundThree.Position.X + mBackgroundThree.Size.Width;
            }

            if (mBackgroundFive.Position.X < -mBackgroundFive.Size.Width)
            {
                mBackgroundFive.Position.X = mBackgroundFour.Position.X + mBackgroundFour.Size.Width;
            }

            Vector2 aDirection = new Vector2(-1, 0);
            Vector2 aSpeed = new Vector2(160, 0);

            mBackgroundOne.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            mBackgroundTwo.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            mBackgroundThree.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            mBackgroundFour.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;            
            mBackgroundFive.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;            
        }

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

            spriteBatch.Begin();

            mBackgroundOne.Draw(this.spriteBatch);
            mBackgroundTwo.Draw(this.spriteBatch);
            mBackgroundThree.Draw(this.spriteBatch);
            mBackgroundFour.Draw(this.spriteBatch);
            mBackgroundFive.Draw(this.spriteBatch);

            mSprite.Draw(this.spriteBatch);
            mSpriteTwo.Draw(this.spriteBatch);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

这就是剩下的代码

"Object reference not set to an instance of an object."

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

namespace XNAdev
{
    class Sprite
    {
        //The size of the Sprite
        public Rectangle Size;

        //Used to size the Sprite up or down from the original image
        public float Scale = 1.0f;

        //The current position of the Sprite
        public Vector2 Position = new Vector2(115, 0);
        //The texture object used when drawing the sprite
        private Texture2D mSpriteTexture;

        //Load the texture for the sprite using the Content Pipeline
        public void LoadContent(ContentManager theContentManager, string theAssetName)
        {
            mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
            Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));
        }

        //Draw the sprite to the screen
        public void Draw(SpriteBatch theSpriteBatch)
        {
            theSpriteBatch.Draw(mSpriteTexture, Position,
                new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), Color.White,
                0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);

        }    
    }
}

I am very new at this C# so any help would be great.

I have no idea what my error is.


namespace XNAdev
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Sprite mSprite;
        Sprite mSpriteTwo;
        Sprite mBackgroundOne;
        Sprite mBackgroundTwo;
        Sprite mBackgroundThree;
        Sprite mBackgroundFour;
        Sprite mBackgroundFive;





        public Game1()
        {           

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

        protected override void Initialize()
        {
            mSprite = new Sprite();
            mSpriteTwo = new Sprite();

            mBackgroundOne = new Sprite();
            mBackgroundOne.Scale = 2.0f;

            mBackgroundTwo = new Sprite();
            mBackgroundTwo.Scale = 2.0f;

            mBackgroundThree = new Sprite();
            mBackgroundThree.Scale = 2.0f;

            mBackgroundFour = new Sprite();
            mBackgroundFour.Scale = 2.0f;

            mBackgroundFive = new Sprite();
            mBackgroundFive.Scale = 2.0f;

            base.Initialize();
        }

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

            mSprite.Position = new Vector2(125, 245);

            mSpriteTwo.LoadContent(this.Content, "SquareGuy");
            mSpriteTwo.Position.X = 300;
            mSpriteTwo.Position.Y = 300;

            mBackgroundOne.LoadContent(this.Content, "Background01");
            mBackgroundOne.Position = new Vector2(0, 0);            

            mBackgroundTwo.LoadContent(this.Content, "Background02");
            mBackgroundTwo.Position = new Vector2(mBackgroundOne.Position.X + mBackgroundOne.Size.Width, 0);

            mBackgroundThree.LoadContent(this.Content, "Background03");
            mBackgroundThree.Position = new Vector2(mBackgroundTwo.Position.X + mBackgroundTwo.Size.Width, 0);

            mBackgroundFour.LoadContent(this.Content, "Background04");
            mBackgroundFour.Position = new Vector2(mBackgroundThree.Position.X + mBackgroundThree.Size.Width, 0);

            mBackgroundFive.LoadContent(this.Content, "Background05");
            mBackgroundFive.Position = new Vector2(mBackgroundFour.Position.X + mBackgroundFour.Size.Width, 0);            
        }

        protected override void UnloadContent()
        {

        }


        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            if (mBackgroundOne.Position.X < -mBackgroundOne.Size.Width)
            {
                mBackgroundOne.Position.X = mBackgroundFive.Position.X + mBackgroundFive.Size.Width;
            }

            if (mBackgroundTwo.Position.X < -mBackgroundTwo.Size.Width)
            {
                mBackgroundTwo.Position.X = mBackgroundOne.Position.X + mBackgroundOne.Size.Width;
            }

            if (mBackgroundThree.Position.X < -mBackgroundThree.Size.Width)
            {
                mBackgroundThree.Position.X = mBackgroundTwo.Position.X + mBackgroundTwo.Size.Width;
            }

            if (mBackgroundFour.Position.X < -mBackgroundFour.Size.Width)
            {
                mBackgroundFour.Position.X = mBackgroundThree.Position.X + mBackgroundThree.Size.Width;
            }

            if (mBackgroundFive.Position.X < -mBackgroundFive.Size.Width)
            {
                mBackgroundFive.Position.X = mBackgroundFour.Position.X + mBackgroundFour.Size.Width;
            }

            Vector2 aDirection = new Vector2(-1, 0);
            Vector2 aSpeed = new Vector2(160, 0);

            mBackgroundOne.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            mBackgroundTwo.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            mBackgroundThree.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            mBackgroundFour.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;            
            mBackgroundFive.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;            
        }

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

            spriteBatch.Begin();

            mBackgroundOne.Draw(this.spriteBatch);
            mBackgroundTwo.Draw(this.spriteBatch);
            mBackgroundThree.Draw(this.spriteBatch);
            mBackgroundFour.Draw(this.spriteBatch);
            mBackgroundFive.Draw(this.spriteBatch);

            mSprite.Draw(this.spriteBatch);
            mSpriteTwo.Draw(this.spriteBatch);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

Thats the rest of the code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暗恋未遂 2024-09-04 15:03:16
  1. 查找如何将 Visual Studio 设置为在出现异常时中断。
  2. 了解如何使用调试器,以便您可以单步执行代码以查看错误发生的位置。
  1. Lookup how to set Visual Studio to break on exceptions.
  2. Learn how to use the debugger so you can step through your code to see where the error occurs.
无声静候 2024-09-04 15:03:16

我仔细查看了它并设法使用验证让它工作,如果您正在绘制的精灵具有空引用(无纹理),它将忽略它并继续绘制其他所有内容。

将 Sprite.cs 中的 Draw() 方法更改为:

//Draw the sprite to the screen
public void Draw(SpriteBatch theSpriteBatch)
{
    if (mSpriteTexture != null)
    {
        theSpriteBatch.Draw(mSpriteTexture, Position,
            new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), Color.White,
            0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
    }
}

出现问题是因为您从未为“Sprite mSprite;”提供纹理,而只提供了位置。

快速片段:

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

    mSprite.Position = new Vector2(125, 245);

    mSpriteTwo.LoadContent(this.Content, "SquareGuy");
    mSpriteTwo.Position.X = 300;
    mSpriteTwo.Position.Y = 300;

正如您所看到的,您只为 mSprite 指定了 125,245 的位置,只需为它分配一个纹理,就像您为其余精灵分配的纹理一样,它就可以正常工作。

不过,在分配纹理后,您不需要从 Draw() 方法中删除 if(mSpriteTexture != null) ,如果您不这样做,则意味着您不会注意到某些内容没有正确分配,如果您稍后要调试其他内容,可能会很痛苦。

I've had a look through it and managed to get it to work using validation, if the sprite you are drawing has a null reference (No texture) it will ignore it and carry on drawing everything else.

Change your Draw() method in the Sprite.cs to this:

//Draw the sprite to the screen
public void Draw(SpriteBatch theSpriteBatch)
{
    if (mSpriteTexture != null)
    {
        theSpriteBatch.Draw(mSpriteTexture, Position,
            new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), Color.White,
            0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
    }
}

The problem occurs because you never give "Sprite mSprite;" a texture, only a position.

Quick snippet:

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

    mSprite.Position = new Vector2(125, 245);

    mSpriteTwo.LoadContent(this.Content, "SquareGuy");
    mSpriteTwo.Position.X = 300;
    mSpriteTwo.Position.Y = 300;

As you can see you only give mSprite a position of 125,245, simply assign it a texture like you have with the rest of the sprites and it will work fine.

You don't need to remove the if(mSpriteTexture != null) from the Draw() method after you assign the texture though, if you don't it simply means you won't notice if something isn't being assigned correctly, could be a pain if you're debugging something else later.

骑趴 2024-09-04 15:03:16

也许您

 mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
 if (mSpriteTexture != null)
     Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));

也应该在 Draw 函数中检查 And 。

Probably you should check

 mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName);
 if (mSpriteTexture != null)
     Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale));

And in the Draw function too.

故事与诗 2024-09-04 15:03:16

此错误意味着在未实例化对象的情况下尝试访问对象的字段或方法之一。

对于您的代码来说,这似乎发生在对象 mSpriteTexture 上。

你应该在某个地方添加

mSpriteTexture = new Texture2D();

,但我无法仅用这段代码来判断在哪里。

This error means that one of the fields or methods of an object has been tried to be accessed without the object being instantiated.

For you code seems that this is happening with the object mSpriteTexture.

You should add

mSpriteTexture = new Texture2D();

somewhere, but I can not tell where only with this piece of code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文