帮助我的重力课(列出问题)

本文关键字:出问题 我的 帮助 | 更新日期: 2023-09-27 18:06:37

在我的主类中,我有一个"实体"对象列表,实体是我创建的一个类,包含玩家和/或敌人所需的所有必要数据。

List<Entity> Listentities = new List<Entity>();

再往下是我的Gravity方法,在其当前状态下存在一些问题。

    public void Gravity(GameTime gameTime)
    {
        foreach(Entity entity in Listentities)
        {
            entity.entityPosition.X += 1;
        }
    }

我在这里要做的很简单;我试图在列表中采取每个实体并移动其位置。每次调用该方法时,X下降一个单位。然而,每次我运行游戏时,我都会在

这行看到"对象引用未设置为对象的实例"。
entity.entityPosition.X += 1;

我需要知道我做错了什么。虽然我学得很快,但我还是个新手。现在,只有一个实体被添加到列表中,即实体"player1"。我可以很容易地通过输入

来修复这个错误
player1.entityPosition.X += 1;

但这只会影响玩家。

编辑:我包括构造函数实体:

    public Entity(string entityName, Texture2D EntityAppearance, int EntityMaxHealth, int SpriteHeight, int SpriteWidth, int CurrentFrame, int AnimationCall, int EntityAttack, int EntityStr, int EntityDex, int EntityLuk, int EntityDefense, bool EntityIsMe )
    {
        this.entityName = entityName;
        this.EntityAppearance = EntityAppearance;
        this.EntityMaxHealth = EntityMaxHealth;
        this.SpriteHeight = SpriteHeight;
        this.SpriteWidth = SpriteWidth;
        this.CurrentFrame = CurrentFrame;
        this.AnimationCall = AnimationCall;
        this.EntityAttack = EntityAttack;
        this.EntityLuk = EntityLuk;
        this.EntityDex = EntityDex;
        this.EntityStr = EntityStr;
        this.EntityDefense = EntityDefense;
        this.EntityIsMe = EntityIsMe;
    }

和game1中的loadcontent()方法

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        camera = new Camera(GraphicsDevice.Viewport);
        player1 = new Entity("player1", Content.Load<Texture2D>("PlaceHolder"), 100, 100, 100, 0, 0, 3, 1, 1, 1, 0, true);
        player1.EntityPosition = new Vector2(0, 0);
        player1.EntityBox = new Rectangle((int)player1.EntityPosition.X, (int)player1.EntityPosition.Y, player1.SpriteWidth, player1.SpriteHeight);
        player1.Origin = new Vector2();
        spritefont = Content.Load<SpriteFont>("SpriteFont1");
    }

和Entity类顶部的entityPosition变量。

    public Vector2 entityPosition;
    public Vector2 EntityPosition
    {
        get { return entityPosition; }
        set { entityPosition = value; }
    }

list的初始化:

    List<Entity> Listentities = new List<Entity>();

这是在game1.cs的顶部

将player1实体添加到Listentities的代码:

    protected override void Initialize()
    {
        InitGraphicsMode(1280, 720, false);
        Listentities.Add(player1);
        base.Initialize();
    }
当然是Game1中的

也许我应该提到我使用的是XNA GS 4.0

帮助我的重力课(列出问题)

也许你没有设置实体的entityPosition的值?

我本来希望entityPosition是一个Point结构,因此不需要初始化,但也许你有自己的entityPosition类。

在这种情况下,

someEntity.entityPosition = new EntityPosition();

确保每个实体在尝试分配给x之前都创建了自己的entityPosition属性。很可能,您忘记在某个构造函数中添加new EntityPosition()

你可以张贴你正在创建实体列表的部分吗?从错误消息中可以清楚地看出,在使用列表之前没有正确地初始化列表。

例如:http://blogs.msdn.com/b/csharpfaq/archive/2004/05/06/why-do-i-get-the-error-object-reference-not-set-to-an-instance-of-an-object.aspx

同样,要调试,只需做一件事-在foreach循环之前在Gravity方法的主体中打印Listentities

确保您的entity和entityPosition不为空…

请确保列表确实已初始化。还要确保没有将null添加到列表中。

你的EntityPosition是一个Vector2 (Struct),因此不能为空。(这是假设您没有创建类并将其命名为vector2)。