【发布时间】:2013-09-04 19:52:05
【问题描述】:
我有一个玩家类,它是一个马里奥角色。 当我向左走时,我调用了一个方法来启动 Left 动画并设置速度。
现在这是我的问题:
我将如何为玩家制作碰撞矩形?
这是我的矩形:
rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
其中使用了我的currentFrame 变量,以及frameWidth 和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);
}
}
那么我将如何在我的播放器类中制作另一个矩形,以便它可以使用碰撞?
提前致谢,如果您需要了解更多信息,请告诉我。
【问题讨论】:
标签: c# animation xna collision