【问题标题】:Animation with Collision?碰撞动画?
【发布时间】:2013-09-04 19:52:05
【问题描述】:

我有一个玩家类,它是一个马里奥角色。 当我向左走时,我调用了一个方法来启动 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);
    }
}

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

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

【问题讨论】:

    标签: c# animation xna collision


    【解决方案1】:
    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。

    【讨论】:

    • 但那是我的问题,我不知道如何绘制 2 个矩形,一个在位置前,一个在我的动画前,因为如果您仔细查看我的代码 new Rectangle() 被我的 currentFrame 占用,这样的:/
    • 然后创建另一个成员Rectangle?我不确定我是否完全理解这个问题。您想绘制两个矩形还是一个矩形用于动画和一个用于碰撞?
    • 两者差不多,因为我有一个动画占据的矩形,但我仍然需要一个矩形来进行瓷砖碰撞,最好的方法是什么?
    • 在您的播放器类中创建一个新成员 Rectangle。定义它,然后用它来测试你的碰撞(就像 joppiesaus 所做的那样)。
    • @UffePuffe 你可以有多个矩形。只需使用矩形来表示碰撞和位置,并使用矩形来绘制动画。
    猜你喜欢
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 2014-12-29
    • 2017-11-07
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    相关资源
    最近更新 更多