2011-04-28 63 views
8

有沒有人有在Silverlight中使用spritesheet的例子?我想剪輯圖像,當按下按鈕時,跳轉到下一幀。 (如果用戶不斷點擊按鈕,它會看起來像一個動畫)。我環顧四周,但還沒有找到我正在尋找的東西。謝謝你的幫助。Silverlight中的Spritesheet

+1

你見過這篇文章嗎?這似乎是大約相同的事情:http://stackoverflow.com/questions/745541/how-does-silverlight-image-clipping-work – LueTm 2011-04-30 18:20:42

回答

12

以下內容將完全符合您的要求。您可以使用鍵盤上的向上鍵和向後鍵在動畫中進行導航。

XAML

<Rectangle x:Name="imgRect"> 
    <Rectangle.Fill> 
     <ImageBrush x:Name="imgBrush" ImageSource="walking_spritesheet.png" Stretch="None" AlignmentX="Left" AlignmentY="Top" />      
    </Rectangle.Fill> 
</Rectangle> 

C#

 imgRect.Width = 240; //Set the width of an individual sprite 
     imgRect.Height = 296; //Set the height of an individual sprite 
     const int ximages = 6; //The number of sprites in each row 
     const int yimages = 5; //The number of sprites in each column 
     int currentRow = 0; 
     int currentColumn = 0; 

     TranslateTransform offsetTransform = new TranslateTransform(); 

     KeyDown += delegate(object sender, KeyEventArgs e) 
     { 
      switch (e.Key) 
      { 
       case Key.Up: 
        currentColumn--; 
        if (currentColumn < 0) 
        { 
         currentColumn = ximages -1; 
         if (currentRow == 0) 
         { 
          currentRow = yimages - 1; 
         } 
         else 
         { 
          currentRow--; 
         } 
        }       
        break; 
       case Key.Down: 
        currentColumn++; 
        if (currentColumn == ximages) 
        { 
         currentColumn = 0; 
         if (currentRow == yimages - 1) 
         { 
          currentRow = 0; 
         } 
         else 
         { 
          currentRow++; 
         } 
        } 
        break; 
       default: 
        break; 
      } 

      offsetTransform.X = -imgRect.Width * currentColumn; 
      offsetTransform.Y = -imgRect.Height * currentRow; 
      imgBrush.Transform = offsetTransform; 

爲了進行測試,請嘗試使用下面的圖片(1440x1480): enter image description here

+0

謝謝,這是偉大的! – Skoder 2011-05-02 18:06:00

+0

@Skoder:太好了。如果您還有其他問題,請告訴我。 – 2011-05-02 19:10:40

1

這裏是另一個解決方案,任何工作精靈表創建,只需添加密鑰代碼。

如果您願意使用Sprite Vortex(實際的特定版本),您可以使用以下類。你必須使用Sprite Vortex 1.2.2,因爲在更新的版本中XML格式被改變了。確保您添加屬性的XML文件更改爲「不編譯」。

如果你需要一個工作的例子,我可以給你一個非常簡單的例子。

p.s. Sprite Vortex應該使用其他程序來做同樣的事情,但是1.2.2版本很麻煩,但不是太糟糕。

該課程在這裏:http://pastebin.com/sNSa7xgQ