Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带碰撞的动画?_C#_Animation_Xna_Collision - Fatal编程技术网

C# 带碰撞的动画?

C# 带碰撞的动画?,c#,animation,xna,collision,C#,Animation,Xna,Collision,我有一个玩家类,是马里奥的角色。 当我向左行走时,我调用一个方法来启动左侧动画并设置速度 现在我的问题是: 如何为玩家制作碰撞矩形? 这是我的矩形: rectangle=新矩形(currentFrame*frameWidth,0,frameWidth,frameHeight) 它使用mycurrentFrame变量以及frameWidth和Height 我还有一个rectanlgeheloper类,如下所示: public static class RectangleHelper {

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

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

它使用my
currentFrame
变量以及
frameWidth
Height

我还有一个
rectanlgeheloper
类,如下所示:

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));
    }
}
如有必要,我的
Map
类将生成Map/level:

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);
    }
}
类映射
{
private List collisionTiles=新列表();
公共列表冲突块
{
获取{return collisionTiles;}
}
私人int宽度、高度;
公共整数宽度
{
获取{返回宽度;}
}
公共内部高度
{
获取{返回高度;}
}
公共映射(){}
公共void生成(int[,]映射,int大小)
{
for(intx=0;x0)
{
添加(新的碰撞块(数字,新的矩形(x*大小,y*大小,大小,大小));
宽度=(x+1)*尺寸;
高度=(y+1)*尺寸;
}
}
}
公共作废抽签(SpriteBatch SpriteBatch)
{
foreach(碰撞瓷砖中的碰撞瓷砖)
瓷砖绘制(spriteBatch);
}
}
那么,我如何在我的player类中创建另一个矩形,以便它可以使用碰撞呢

提前谢谢你,如果你还需要知道什么,告诉我

rectangle = new Rectangle(playerPosition.X, playerPosition.Y, playerTexture.Width, playerTexture.Height);
如果要检测碰撞,请在“更新无效”中执行以下操作:

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;
矩形
用于碰撞,而不是绘图。
animationRectangle
用于显示动画和绘图。
所以你只画动画矩形。

但这是我的问题,我不知道如何画两个矩形,一个在位置前面,一个在我的动画前面,因为如果你仔细看我的代码,新的矩形()被我的当前帧占据,这样:/n然后创建另一个成员
矩形
?我不确定我是否完全理解这个问题。你想画两个矩形还是一个矩形用于动画,一个矩形用于碰撞?几乎两者都有,因为我有一个矩形用于动画,但我仍然需要一个矩形用于平铺碰撞,最好的方法是什么?在你的播放器类中创建一个新成员,它是
矩形
。定义它,然后使用它来测试您的碰撞(就像joppiesaus所做的那样)。@UffePuffe您可以有多个矩形。只需使用一个用于碰撞和位置的矩形,以及一个用于绘制动画的矩形。
Rectangle rectangle;
Rectangle animationRectangle;