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