帮助我的重力课(列出问题)
在我的主类中,我有一个“实体”对象列表,实体是我用玩家和/或敌人所需的所有必要数据创建的类。
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许您没有设置实体的entityPosition 的值?
我希望entityPosition是一个点结构并且因此不需要初始化,但也许您有自己的 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,
确保每个实体在尝试分配给 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.您可以发布您创建实体列表的部分吗?从错误消息中可以清楚地看出,您在使用列表之前没有正确初始化该列表。
例如,看看这个: 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.确保您的实体和实体位置不为空...
Make sure your entity and entityPosition is not null...
请确保列表确实已初始化。另请确保不要将 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).