0

私は現在、XNA用のFarseerPhysicsを使用したゲームプロジェクトに取り組んでいます。現在、Farseerに付属するBodyクラスを拡張する2つのクラスがあります。以下は、それらを衝突させるための私のコードです。

以下のクラスは少し自明です。基本的には、プレイヤーが世界中のすべてのタイルと衝突できるようにしたいと思います。

    public Player(World gameWorld, GameWindow Window, int playernum, Texture2D sprite) : base(gameWorld)
    {
        //place the player in the center of the screen - this whole method can be changed
        Position = new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
        playerSprite = sprite;
        playerNum = (PlayerIndex)playernum;

        //Fixture stuff
        playerFixture = FixtureFactory.AttachRectangle(sprite.Width, sprite.Height, 1, new Vector2(), this);
        playerFixture.CollisionCategories = Category.Cat2;
        playerFixture.CollidesWith = Category.Cat1;
        playerFixture.OnCollision += playerOnCollision;
        //initialize the melee weapon
        //initialize the ranged weapon
    }

    public Tile(World gameWorld, Vector2 location, Game1 game, Vector2 offset) : base(gameWorld)
    {
        //Loading content in the constructor for simplicity's sake because the content manager is initialized by the time the stage is created
        health = 100;
        prevhealth = health;
        maxhealth = health;

        this.game = game;
        contentName = game.random.NextDouble() > 0.5 ? "Images/Tiles/MarbleTilesBreak" : "Images/Tiles/MarbleTiles1Break";
        tileTex = game.Content.Load<Texture2D>(contentName + "0");
        //breakSound = game.Content.Load<SoundEffect>("Tiles/FloorBreaking");
        location.X *= tileTex.Width;
        location.Y *= tileTex.Height;
        location += offset;
        Position = location;
        tileFixture = FixtureFactory.AttachRectangle(tileTex.Width, tileTex.Height, 1, new Vector2(), this);
        tileFixture.CollisionCategories = Category.Cat1;
        tileFixture.CollidesWith = Category.Cat2;
        tileFixture.OnCollision += _OnCollision;
    }

私の_OnCollisionは次のようになります。

    public bool _OnCollision(Fixture fix1, Fixture fix2, Contact con)
    {
        if (fix2.CollisionCategories == Category.Cat2)
        {
            health -= 10f;
        }
      return false;
    }

しかし、コードを実行しても、衝突の兆候はありません。タイルのヘルスが0の場合、タイルを削除する必要がありますが、タイルは削除されません。

4

1 に答える 1

0

私が見つけたのは、スプライトを移動するには、コメントで言及されているように世界をステップし、スプライトの位置をボディの位置に割り当てる必要があることでした。これにより、世界が更新されました。

world.step(TIME_SPEED);
sprite.Position = body.Position;
于 2012-07-27T01:39:43.160 に答える