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

发布于 2024-12-02 00:10:35 字数 2843 浏览 1 评论 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;

但这只会影响玩家。

编辑:我在 game1 中包含了 Entity: 的构造函数

    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;
    }

和 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");
    }

以及实体类顶部的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

In my main class, I have a list of "Entity" objects, Entity being a class I have created with all the necessary data I need for a player and/or enemies.

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

Farther down I have my Gravity method which, in its current state has some problems.

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

It's very simple what I'm trying to do here; I am trying to take every Entity in the list and move its position.X down one unit every time the method is called. However, Every time I run the game, I get an "Object reference not set to an instance of an object" on the line

entity.entityPosition.X += 1;

I need to know what I'm doing wrong. While I am learning quickly, I am still rather new.
Right now, only one entity is ever added to the list, the Entity "player1". I can easily fix this error by typing

player1.entityPosition.X += 1;

but that would only ever affect the player.

Edit: I am including the constructor for Entity:

    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;
    }

and the loadcontent() method in game1.

    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");
    }

and the entityPosition variables at the top of my Entity class.

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

The initialization of the list:

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

this is at the top of game1.cs

The code where I add the player1 Entity to Listentities:

    protected override void Initialize()
    {
        InitGraphicsMode(1280, 720, false);
        Listentities.Add(player1);
        base.Initialize();
    }

in Game1 of course.

Maybe I should have mentioned also that I am using XNA GS 4.0

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

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

发布评论

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

评论(5

我不在是我 2024-12-09 00:10:35

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

我希望entityPosition是一个点结构并且因此不需要初始化,但也许您有自己的 EntityPosition 类。

在那种情况下,

someEntity.entityPosition = new EntityPosition();

Perhaps you are not setting the value of the entity's entityPosition?

I would have expected entityPosition to be a Point structure and therefore not need initialization, but maybe you have your own EntityPosition class.

In that case,

someEntity.entityPosition = new EntityPosition();
小清晰的声音 2024-12-09 00:10:35

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

Ensure that each entity has created its entityPosition property before attempting to assign to X. Likely, you have forgotten to add a new EntityPosition() in one of your constructors somewhere.

骑趴 2024-12-09 00:10:35

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

例如,看看这个: 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

Can you post the part where you are creating the entities list? From the error message it is clear that you haven't initialized the list properly before using it.

For example look at this: 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

Also, to debug, just do one thing - print Listentities in the body of the method Gravity before the foreach loop.

负佳期 2024-12-09 00:10:35

确保您的实体和实体位置不为空...

Make sure your entity and entityPosition is not null...

寒冷纷飞旳雪 2024-12-09 00:10:35

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

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

Please ensure that the list is indeed initialized. Also make sure that you don't add null to the list.

Your EntityPosition is a Vector2 (Struct) and therefore cannot be null. (That is ofc assuming that you didn't create a class and named it vector2).

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