Frame Based Sprite Animation in Silverlight

本文将介绍如何在Silverlight中实现帧基精灵动画,包括创建动画所需的步骤、关键概念以及具体代码示例。通过结合“精灵条”和裁剪区域的方法,展示了一种可能的帧基动画实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Frame Based Sprite Animation in Silverlight

Code for this sample: http://www.bluerosegames.com/silverlight-games-101/samples/wokka.zip

There is more than one way to skin a cat. In Silverlight, many time there are several, with many being bad, and a couple being good. Sometimes there is no clear cut winner as to the best way to do something. This is one of those cases, and I'm presenting one out of many ways of potentially doing frame based sprite animation. Some other ways would be to use an ImageBrush or single images. The technique I have chosen is a combination of a "sprite strip" and a clipping region.

Frame based animation is what they use to develop cartoons, it's been a part of video games since the days of Donkey Kong and earlier. The basic idea is that you flip through a series of images to produce the illusion of motion.

For this sample, let's create a new Silverlight Application called WokkaAnimation, and add the BlueRoseGames.Helpers project to the solution. This is the same one that we used in the previous SpaceRocks sample, or get it from the sample zip file at the top of this post. Then in the WokkaAnimation project, add a reference to BlueRoseGames.Helpers.

Modify the Page.xaml to be 640x480 with a Black background:

<UserControl x:Class="WokkaAnimation.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="640" Height="480">
    <Canvas x:Name="LayoutRoot" Background="Black">
 
    </Canvas>
</UserControl>

Now create a new UserControl called WokkaSpriteContent.xaml. We need an image to animate, so here it is:

wokka

Any resemblance to a certain Atari character is strictly coincidental. Add this image to the project as wokka.png

This image is 500x100, and we'll want the sprite to be 100x100. Change the WokkaSpriteContent.xaml as follows:

<UserControl x:Class="WokkaAnimation.WokkaSpriteContent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="100" Height="100">
    <Canvas x:Name="LayoutRoot">
        <Image x:Name="image" Source="wokka.png"/>
    </Canvas>
</UserControl>

Then to create this sprite on our main canvas. Here's the Page.xaml.cs:

using System.Windows.Controls;
using BlueRoseGames.Helpers.Sprites;
 
namespace WokkaAnimation
{
    public partial class Page : UserControl
    {
        WokkaSpriteContent spriteContent;
        Sprite wokka;
 
        public Page()
        {
            InitializeComponent();
            spriteContent = new WokkaSpriteContent();
            wokka = new Sprite(spriteContent);
            LayoutRoot.Children.Add(wokka);
        }
    }
}

This should look as follows:

wokkacanvas

Now we need to hide the images that we don't want to see for the current frame. This is done with the Clip property. Most elements accept a Clip property, in our case we'll clip the Canvas that is the parent of the Image in our WokkaSpriteContent.xaml:

<UserControl x:Class="WokkaAnimation.WokkaSpriteContent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="100" Height="100">
    <Canvas x:Name="LayoutRoot">
        <Canvas.Clip>
            <RectangleGeometry Rect="0,0,100,100"/>
        </Canvas.Clip>
        <Image x:Name="image" Source="wokka.png"/>
    </Canvas>
</UserControl>
There are other options for the Clip property besides a RectangleGeometry, like an Ellipse, Path, or Line.

Now you should only see one frame of the sprite.

For the animation, we can reposition the Image so that the part we want is showing through the Clip region. The formula for the value of the Canvas.Left property is:

left = - (frame index * frame width)

Here's WokkaSpriteContent.xaml.cs after adding an Update method to do this:

using System;
using System.Windows.Controls;
 
namespace WokkaAnimation
{
    public partial class WokkaSpriteContent : UserControl
    {
        TimeSpan animationDelay = TimeSpan.FromSeconds(.05);
        TimeSpan timeSinceLast = TimeSpan.Zero;
        int currentFrame = 0;
        int delta = 1;
        public WokkaSpriteContent()
        {
            InitializeComponent();
        }
 
        public void Update(TimeSpan elapsed)
        {
            timeSinceLast += elapsed;
            if (timeSinceLast > animationDelay)
            {
                timeSinceLast -= animationDelay;
                currentFrame+=delta;
                if (currentFrame == 4) delta = -1;
                if (currentFrame == 0) delta = 1;
                image.SetValue(Canvas.LeftProperty, currentFrame * -100d);
            }
        }
    }
}

In this case, we'll cycle from frame 0 to frame 4 then go back to 0, and repeat. The changing of frame will happen every .05 seconds. Now all we have to do is call the WokkaSpriteContent's from a game loop. Here is the finished Page.xaml.cs:

using System.Windows.Controls;
using BlueRoseGames.Helpers.Sprites;
using BlueRoseGames.Helpers.Timers;
 
namespace WokkaAnimation
{
    public partial class Page : UserControl
    {
        WokkaSpriteContent spriteContent;
        Sprite wokka;
        CompositionTargetGameLoop gameLoop;
 
        public Page()
        {
            InitializeComponent();
            spriteContent = new WokkaSpriteContent();
            wokka = new Sprite(spriteContent);
            LayoutRoot.Children.Add(wokka);
            gameLoop = new CompositionTargetGameLoop();
            gameLoop.Update += new GameLoop.UpdateHandler(gameLoop_Update);
            gameLoop.Start();
        }
 
        void gameLoop_Update(object sender, System.TimeSpan elapsed)
        {
            spriteContent.Update(elapsed);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值