2013-09-01 207 views
0

我有一個球員類,這是一個馬里奧字符。 當我走到左邊時,我會調用一個方法來啓動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); 
    } 
} 

那麼我該如何去製作我的播放器類中的另一個矩形,以便它可以使用碰撞?

在此先感謝您,如果您需要了解更多信息,請告訴我。

回答

2
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; 

rectangle被用於碰撞,不適用於繪圖。 animationRectangle獲取用於顯示動畫和繪圖。 所以你只繪製animationRectangle。

+0

但是多數民衆贊成我的問題,我不知道如何繪製2個矩形,一個前面的位置,一個前面我的動畫,如果你仔細觀察我的代碼,_new Rectangle()_被我的_currentFrame_佔用,等:/ –

+0

然後創建另一個成員'Rectangle'?我不確定我完全理解了這個問題。你想要拖出兩個矩形,還是有一個矩形用於動畫,一個用於碰撞? –

+0

幾乎都是這樣,cus我有一個動畫佔據的矩形,但是我仍然需要一個矩形來實現拼接,那麼最好的方法是什麼? –