1

私はC#を試しているC++プログラマーです。私はc++でbox2dを使ってたくさんのことをしましたが、c#を使うのはこれが初めてです。だから、私は遠洋物理エンジンで簡単なゲームを作ろうとしています。コードをコンパイルしようとすると(Visual Studio C#2010ExpressとXNAGame Studio 4.0を使用しています)、body.csでIBroadPhase broadPhase = World.ContactManager.BroadPhase;次のエラーで停止しますnullreferenceexception was unhandled。問題は私の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);
    }
}

farseerテストベッドは正常に動作するので、私のコードが問題であり、farseerではないと確信しています。私のコードをもっと見る必要があるかどうか教えてください。ファーザーフォーラムにも投稿しましたので、回答がありましたらお知らせします。前もって感謝します。

4

1 に答える 1

0

誰かが私にGame1.csのコードを表示するように頼み、問題を見つけました。問題は、プレーヤーを初期化する前に世界を構築したことがなかったということでした。world = new World(Vector2.Zero);プレーヤーを初期化する前に追加することで修正されました。

于 2011-10-08T05:34:30.427 に答える