c# - Silverlight 中的 Spritesheet

标签 c# silverlight animation clip sprite-sheet

有没有人有在 Silverlight 中使用 spritesheet 的例子?我想剪辑图像,然后在按下按钮时跳转到下一帧。 (如果用户一直点击按钮,它看起来就像一个动画)。我环顾四周,但没有找到我要找的东西。感谢您的帮助。

最佳答案

以下将完全满足您的需求。您可以使用键盘上的向上 和向下 键在动画中向前和向后导航。

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

关于c# - Silverlight 中的 Spritesheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5812752/

相关文章:

ios - 动画化多个类似uiscrollviews的动画

listview - 使用 React Native LayoutAnimation 对 ListView 行删除进行动画处理

c# - 如何通过列表属性将对象拆分为对象列表 C#

c# - 使用导航时将值从另一个页面传递到 Silverlight 中的 MainPage.xaml?

jquery - 使用 JQuery 透视查看器

silverlight - 标准 .Net 2 dll 是否与 silverlight 2.0 运行时兼容?

r - ggplotly 与直方图 - 无法使其工作

c# - 一种更优雅的编写决策代码的方式来评估具有不同优先级的多个输入?

c# - 验证 JWT 出现奇怪的 “Unable to match key kid” 错误

c# - 对按钮单击事件使用一种方法