0
我有一個球員類,這是一個馬里奧字符。 當我走到左邊時,我會調用一個方法來啓動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);
}
}
那麼我該如何去製作我的播放器類中的另一個矩形,以便它可以使用碰撞?
在此先感謝您,如果您需要了解更多信息,請告訴我。
但是多數民衆贊成我的問題,我不知道如何繪製2個矩形,一個前面的位置,一個前面我的動畫,如果你仔細觀察我的代碼,_new Rectangle()_被我的_currentFrame_佔用,等:/ –
然後創建另一個成員'Rectangle'?我不確定我完全理解了這個問題。你想要拖出兩個矩形,還是有一個矩形用於動畫,一個用於碰撞? –
幾乎都是這樣,cus我有一個動畫佔據的矩形,但是我仍然需要一個矩形來實現拼接,那麼最好的方法是什麼? –