0

我有一个玩家类,这是一个马里奥角色。当我向左走时,我调用了一个方法来启动Left动画并设置速度。

现在这是我的问题:我将如何为玩家制作碰撞矩形?这是我的矩形: rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);

它使用我的currentFrame变量和frameWidthand Height

我也有一个RectanlgeHelper看起来像这样的类:

public static class RectangleHelper
{

    public static bool TouchTopOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Bottom >= r2.Top - 1 &&
                r1.Bottom <= r2.Top + (r2.Height / 2) &&
                r1.Right >= r2.Left + r2.Width / 5 &&
                r1.Left <= r2.Right - r2.Height / 6);
    }

    public static bool TouchBottomOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Top <= r2.Bottom + (r2.Height / 5) &&
                r1.Top >= r2.Bottom - 1 &&
                r1.Right >= r2.Left + r2.Width / 5 &&
                r1.Left <= r2.Right - r2.Width / 5);
    }

    public static bool TouchLeftOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Right <= r2.Right &&
                r1.Right >= r2.Left - 5 &&
                r1.Top <= r2.Bottom - (r2.Width / 4) &&
                r1.Bottom >= r2.Top + (r2.Width / 4));  
    }

    public static bool TouchRightOf(this Rectangle r1, Rectangle r2)
    {
        return (r1.Left >= r2.Right &&
                r1.Left <= r2.Right + 5 &&
                r1.Top <= r1.Bottom - (r2.Width / 4) &&
                r1.Bottom >= r2.Top + (r2.Width / 4));
    }
}

在我的Tile课堂上,它在地图上绘制瓷砖:

class Tiles
{
    protected Texture2D texture;

    private Rectangle rectangle;

    public Rectangle Rectangle
    {
        get { return rectangle; }
        protected set { rectangle = value; }
    }

    private static ContentManager content;
    public static ContentManager Content
    {
        protected get { return content; }
        set { content = value; }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, rectangle, Color.White);
    }

}

class CollisionTiles : Tiles
{
    public CollisionTiles(int i, Rectangle newRectangle)
    {
        texture = Content.Load<Texture2D>("Tiles/Tile" + i);
        this.Rectangle = newRectangle;
    }
}

如有必要,我的Map班级会生成地图/关卡:

class Map
{
    private List<CollisionTiles> collisionTiles = new List<CollisionTiles>();

    public List<CollisionTiles> CollisionTiles
    {
        get { return collisionTiles; }
    }

    private int width, height;
    public int Width
    {
        get { return width; }
    }
    public int Height
    {
        get { return height; }
    }

    public Map() { }

    public void Generate(int[,] map, int size)
    {
        for (int x = 0; x < map.GetLength(1); x++)
            for (int y = 0; y < map.GetLength(0); y++)
            {
                int number = map[y, x];

                if (number > 0)
                {
                    CollisionTiles.Add(new CollisionTiles(number, new Rectangle(x * size, y * size, size, size)));

                    width = (x + 1) * size;
                    height = (y + 1) * size;
                }
            }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        foreach (CollisionTiles tile in collisionTiles)
            tile.Draw(spriteBatch);
    }
}

那么我将如何在我的播放器类中制作另一个矩形,以便它可以使用碰撞?

在此先感谢您,如果您需要了解更多信息,请告诉我。

4

1 回答 1

2
rectangle = new Rectangle(playerPosition.X, playerPosition.Y, playerTexture.Width, playerTexture.Height);

当您想检测碰撞时,请在 update void 中执行此操作:

player.animationRectangle.X = player.rectangle.X;
player.animationRectangle.Y = player.rectangle.Y;
foreach (CollistionTiles tile in map.CollisionTiles)
{
   if (player.rectangle.TouchLeftOf(tile.Rectangle))
   {
      //touched left of tile
   }
}

编辑:在您的播放器类中,创建两个成员:

Rectangle rectangle;
Rectangle animationRectangle;

rectangle用于碰撞,而不是用于绘图。 animationRectangle用于显示动画和绘图。所以你只画a​​nimationRectangle。

于 2013-09-01T17:28:33.907 回答