Sko*_*der 8 c# silverlight animation sprite-sheet clip
有没有人有一个在Silverlight中使用spritesheet的例子?我想剪辑图像,按下按钮时,跳转到下一帧.(如果用户不断点击按钮,它看起来就像一个动画).我环顾四周但却找不到我正在寻找的东西.谢谢你的帮助.
Nak*_*nch 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>
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
要进行测试,请尝试使用以下图像(1440x1480):
