2011-08-26 45 views
0

在我的主类中,我有一个“实体”对象的列表,实体是我创建的一个类,具有我需要的所有必要的数据给玩家和/或敌人。帮助我的重力类(列表问题)

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

更进一步我有我的重力方法,其目前的状态有一些问题。

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

这是非常简单的,我想在这里做;我正在尝试将列表中的每个实体都移动,并在每次调用该方法时将其移动一个单位。然而,每次我运行游戏,我得到一个“对象引用不设置到对象的实例”就行了

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

和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

+0

你能否提供用于添加实体的代码,以及可能的实体类?我的猜测是无法正确初始化,无论是在创建实体还是将其添加到列表时。或者entityPosition是一个私人领域,不能以这种方式访问​​。 – Opieus

+0

只是别的东西:而不是使用新的Vector2(0,0),你可以做Vector2.Zero(实际上不需要,因为vector2是一个结构,因此有一个默认的初始化,它等于Vector2.Zero) – Maxem

+0

请发布代码将您的玩家(玩家)添加到列表中的代码。我假设你在初始化之前添加播放器。 – Maxem

回答

0

确保每个实体试图指派为X.有可能的,你忘了在一个加new EntityPosition()之前创建的entityPosition你的财产某处的构造函数。

1

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

我会期望entityPosition是一个Point structure,因此不需要初始化,但也许你有你自己的EntityPosition类。

在这种情况下,

someEntity.entityPosition = new EntityPosition(); 
+0

我已经在实体中包含结构以及上面有关实体位置的行。每个实体的位置等都将在playercontent()方法中与player1一起初始化。 – Zanrkvil

0

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

0

请确保列表确实初始化。另外请确保您不要将空值添加到列表中。

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