silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Frame Based Sprite Animation in 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 ti

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.

@H_855_20@modify the Page.xaml to be 640x480 with a Black BACkground:

<UserControl x:Class="WokkaAnimation.Page"
    xmlns="http://scheR_588_11845@as.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://scheR_588_11845@as.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:

Frame Based Sprite Animation in Silverlight

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:

"WokkaAnimation.WokkaSpriteContent"
"100" Height="100">
"LayoutRoot">
        <Image x:Name="image" source="wokka.png"/>
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:

Frame Based Sprite Animation in Silverlight

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:

<Canvas.Clip>
            <RectangleGeometry Rect="0,100,100"/>
        </Canvas.Clip>
"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;
class WokkaSpriteContent : UserControl
        TimeSpan animationDelay = TimeSpan.FromSeconds(.05);
        TimeSpan timeSinceLast = TimeSpan.Zero;
int currentFrame = 0;
int delta = 1;
public WokkaSpriteContent()
        {
            InitializeComponent();
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 BlueRoseGames.Helpers.Timers;
namespace WokkaAnimation
{
class Page : UserControl
    {
        WokkaSpriteContent spriteContent;
        Sprite wokka;
        CompositionTargetGameLoop gameLoop;
            gameLoop = new CompositionTargetGameLoop();
            gameLoop.update += new GameLoop.updateHandler(gameLoop_updatE);
            gameLoop.Start();
void gameLoop_update(object sender,System.TimeSpan elapsed)
            spriteContent.update(elapsed);
}

大佬总结

以上是大佬教程为你收集整理的Frame Based Sprite Animation in Silverlight全部内容,希望文章能够帮你解决Frame Based Sprite Animation in Silverlight所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。