碰撞动画?

时间:2013-09-01 17:02:39

标签: c# animation xna collision

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

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

使用我的currentFrame变量以及frameWidthHeight

我还有一个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);
    }
}

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

提前致谢,如果您还需要了解更多信息,请告诉我。

1 个答案:

答案 0 :(得分: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用于显示动画和绘图。 所以你只绘制animationRectangle。