开发者

Help with my gravity class (list problems)

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


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();


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.


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.


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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜