Spritesheet in Silverlight

前端 未结 2 1171
既然无缘
既然无缘 2020-12-16 03:49

Does anyone have an example of using a spritesheet in Silverlight? I\'d like to clip the image and, when a button is pressed, jump to the next frame. (If the user keeps tapp

相关标签:
2条回答
  • 2020-12-16 04:20

    Here is another solution that works with any sprite sheet you create, just add the key code.

    If you are willing to use Sprite Vortex (a specific version actually) you can use the following class. You have to use Sprite Vortex 1.2.2 because in the newer versions the XML format is changed. Make sure that the XML file you add the property is changed to "Do not compile".

    If you need a working example I can send you a very simple one.

    p.s. Sprite Vortex should do the same thing you use the other program for, however v 1.2.2 is pretty buggy but not too bad.

    the class is here : http://pastebin.com/sNSa7xgQ

    0 讨论(0)
  • 2020-12-16 04:40

    The following will do exactly what you're looking for. You can use the Up and Down keys on your keyboard to navigate forwards and backwards through the animation.

    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;
    

    For testing, try using the following image (1440x1480): enter image description here

    0 讨论(0)
提交回复
热议问题