Silverlight制作逐帧动画 v2 - part2

Silverlight制作逐帧动画 v2 - part2

接上一篇《Silverlight制作逐帧动画 v2 - part1》,在这里完善了一下算法,加入了fps的机制进去。

        private string[] shippics = new string[] { "Ship1/1.png""Ship1/2.png""Ship1/3.png""Ship1/4.png" };
        
protected DateTime lastTick;
        
protected TimeSpan elapsed;


        
private double lasrFrameTime = 0;
        
private int currentFrame = 0;

        
private double fps = 15;

        
public MainPage()
        {
            InitializeComponent();
            lastTick 
= DateTime.Now;
            CompositionTarget.Rendering 
+= new EventHandler(CompositionTarget_Rendering);
        }

        
void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            DateTime now 
= DateTime.Now;
            elapsed 
= now - lastTick;
            lastTick 
= DateTime.Now;

            lasrFrameTime 
+= elapsed.TotalSeconds;


            
if (lasrFrameTime >1 / fps))
            {
                lasrFrameTime 
= 0;

                currentFrame
++;
                currentFrame 
%= shippics.Length;
                RenderShip();
            }
        }

        
private void RenderShip()
        {
            var imageBrush 
= new ImageBrush();
            imageBrush.Stretch 
= Stretch.None;
            imageBrush.AlignmentX 
= AlignmentX.Left;
            imageBrush.AlignmentY 
= AlignmentY.Top;
            imageBrush.ImageSource 
= new BitmapImage(new Uri("/slShipAnimate;component/" + shippics[currentFrame], UriKind.Relative));
            myShip.Fill 
= imageBrush;
        }
posted @ 2009-09-12 02:05  王喆(nasa)  阅读(1617)  评论(0编辑  收藏  举报