0

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

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

再往下,我有我的重力方法,在目前的状态下有一些问题。

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

我在这里尝试做的事情非常简单;每次调用该方法时,我都会尝试获取列表中的每个实体并将其 position.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<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

4

5 回答 5

1

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

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

在这种情况下,

someEntity.entityPosition = new EntityPosition();
于 2011-08-26T00:31:25.233 回答
0

在尝试分配给 X 之前,确保每个实体都已创建其 entityPosition 属性。您可能忘记new EntityPosition()在某个构造函数中添加一个。

于 2011-08-26T00:29:38.843 回答
0

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

例如看看这个:http: //blogs.msdn.com/b/csharpfaq/archive/2004/05/06/why-do-i-get-the-error-object-reference-not-set-to-对象实例.aspx

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

于 2011-08-26T00:32:38.940 回答
0

确保您的实体和 entityPosition 不为空...

于 2011-08-26T00:35:48.393 回答
0

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

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

于 2011-08-26T00:38:34.170 回答