简体   繁体   English

Farseer物理引擎的body.cs中“未处理nullreferenceexception”

[英]“nullreferenceexception was unhandled” in body.cs of farseer physics engine

I'm a c++ programmer trying out c#. 我是尝试C#的C ++程序员。 I've done a lot with box2d in c++, but this is my first time with c#. 我在C ++中使用box2d做了大量工作,但这是我第一次使用c#。 So, I'm trying to make a simple game with farseer physics engine. 因此,我正在尝试使用Farseer物理引擎制作一个简单的游戏。 When I try to compile my code (I'm using visual studio c# 2010 express and xna game studio 4.0), it stops in body.cs at IBroadPhase broadPhase = World.ContactManager.BroadPhase; 当我尝试编译代码(我使用Visual Studio C#2010 Express和XNA Game Studio 4.0)时,它停止在body.cs中,位于IBroadPhase broadPhase = World.ContactManager.BroadPhase; With this error: nullreferenceexception was unhandled . 出现此错误: nullreferenceexception was unhandled I believe the problem is in my Player class, so here's the code for that: 我相信问题出在我的Player类中,因此这是代码:

public class Player : Entity
{
    Vector2 position;
    SpriteBatch spriteBatch;
    Texture2D texture;
    Rectangle rectangle;
    Body body;
    CircleShape shape;
    Fixture fixture;
    public Player()
    {
        // TODO: Construct any child components here
    }

    /// 
    /// Allows the game component to perform any initialization it needs to before starting
    /// to run.  This is where it can query for any required services and load content.
    /// 
    public override void Initialize(World world, SpriteBatch spriteBatch, Texture2D texture)
    {
        // TODO: Add your initialization code here
        this.spriteBatch = spriteBatch;
        this.texture = texture;
        rectangle = new Rectangle(0, 0, 11, 14);
        body = BodyFactory.CreateBody(world);
        body.BodyType = BodyType.Dynamic;
        body.Position = new Vector2(0, 0);
        shape = new CircleShape(1.0f, 1.0f);
        fixture = body.CreateFixture(shape);
        base.Initialize(world, spriteBatch, texture);
    }

    /// 
    /// Allows the game component to update itself.
    /// 
    /// <param name="gameTime" />Provides a snapshot of timing values.
    public override void Update(GameTime gameTime)
    {
        // TODO: Add your update code here

        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
        spriteBatch.Begin();
        spriteBatch.Draw(texture, new Vector2(body.WorldCenter.X * 10, body.WorldCenter.Y * 10), rectangle, Color.White);
        spriteBatch.End();
        base.Draw(gameTime);
    }
}

The farseer testbed runs fine, so I'm pretty sure my code is the problem, and not farseer. Farseer测试平台运行良好,因此我可以肯定我的代码是问题所在,而不是Farseer。 Let me know if you need to see more of my code. 让我知道您是否需要查看更多我的代码。 I've also posted this in the farseer forums, so If I get an answer there, I'll let you guys know. 我也将其发布在farseer论坛中,因此,如果在那里得到答案,我会告诉大家。 Thanks in advance. 提前致谢。

Someone asked me to show the code of Game1.cs, and found the problem. 有人要求我显示Game1.cs的代码,然后发现了问题。 The problem was that I had never constructed the world before initializing my player. 问题是我在初始化播放器之前从未构建过世界。 It was fixed by adding world = new World(Vector2.Zero); 通过添加world = new World(Vector2.Zero); before I initialized the player. 在初始化播放器之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM