Frame Based Sprite Animation in Silverlight

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

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);
        }
    }
}
本资源集提供了针对小型无人机六自由度非线性动力学模型的MATLAB仿真环境,适用于多个版本(如2014a、2019b、2024b)。该模型完整描述了飞行器在三维空间中的六个独立运动状态:绕三个坐标轴的旋转(滚转、俯仰、偏航)与沿三个坐标轴的平移(前后、左右、升降)。建模过程严格依据牛顿-欧拉方程,综合考虑了重力、气动力、推进力及其产生的力矩对机体运动的影响,涉及矢量运算与常微分方程求解等数学方法。 代码采用模块化与参数化设计,使用者可便捷地调整飞行器的结构参数(包括几何尺寸、质量特性、惯性张量等)以匹配不同机型。程序结构清晰,关键步骤配有详细说明,便于理解模型构建逻辑与仿真流程。随附的示例数据集可直接加载运行,用户可通过修改参数观察飞行状态的动态响应,从而深化对无人机非线性动力学特性的认识。 本材料主要面向具备一定数学与编程基础的高校学生,尤其适合计算机、电子信息工程、自动化及相关专业人员在课程项目、专题研究或毕业设计中使用。通过该仿真环境,学习者能够将理论知识与数值实践相结合,掌握无人机系统建模、仿真与分析的基本技能,为后续从事飞行器控制、系统仿真等领域的研究或开发工作奠定基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值