简体   繁体   English

多种动画Windows Phone 7

[英]Multiple Animations Windows Phone 7

I am trying to make one-hand machine game. 我正在尝试制作单手机器游戏。 To animate falling down Images I am using storyboard. 为使掉落的图像动画,我正在使用情节提要。 My question is if someone know how to make multiple images. 我的问题是是否有人会制作多张图像。 In this code I have just on image falling down. 在这段代码中,我只是图像掉落。 Somone know how to make eg 100 images animation in the storyboard? Somone知道如何在情节提要中制作100张图像动画吗?

    private Storyboard CreateStoryBoard()
    {
        Storyboard sb = new Storyboard();

        DoubleAnimation firstAnimation = new DoubleAnimation();
        firstAnimation.SpeedRatio = 8;
        firstAnimation.From = 0;
        firstAnimation.To = 600;
        firstAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));




        Storyboard.SetTarget(firstAnimation, Okejka);
        Storyboard.SetTargetProperty(firstAnimation, new PropertyPath("(Canvas.Top)"));
        sb.Children.Add(firstAnimation);
        return sb;
    }


  private void SpinButton_Click(object sender, RoutedEventArgs e)
    {
        Storyboard sb = CreateStoryBoard();
        sb.Begin();
    }

For each object you need create DoubleAnimation, each DoubleAnimation add to one Storyboard and then play it. 对于需要创建DoubleAnimation的每个对象,将每个DoubleAnimation添加到一个Storyboard中,然后进行播放。

This is one method of my AnimationHelper, I modified it for this case. 这是我的AnimationHelper的一种方法,在这种情况下,我对其进行了修改。

    public static void Animate(List<DependencyObject> objects, EventHandler onComplete = null)
    {
        Storyboard sb = new Storyboard();
        foreach (DependencyObject obj in objects)
        {
            DoubleAnimation da = new DoubleAnimation();
            da.From = FromValue; // Set you From value
            da.To = ToValue; // Set your To value
            da.Duration = new Duration(TimeSpan.FromSeconds(2)); // Set your Duration
            // a.EasingFunction = anim.Func; Easing function
            // da.BeginTime = anim.BeginTime; Begin time for each DA
            Storyboard.SetTarget(da, obj);
            Storyboard.SetTargetProperty(da, new PropertyPath(/* this your Property path */));
            sb.Children.Add(da);
        }                        
        if (onComplete != null)
            sb.Completed += onComplete;
        sb.Begin();
    }

UPDATE #1 The next code is Button.Click event handler, this code creates 20 Images and adds it to Canvas, next step is create animations for each Image using one instance of Storyboard. 更新#1下一个代码是Button.Click事件处理程序,此代码创建20个图像并将其添加到Canvas,下一步是使用一个Storyboard实例为每个图像创建动画。

    private async void b1_Click(object sender, RoutedEventArgs e)
    {
        CanvasContainer.Children.Clear();
        _images = new List<Image>();

        // load bitmap
        BitmapImage bmp = new BitmapImage(new Uri("Assets/appbar/appbar.italic.png", UriKind.Relative));

        // create 20 Image instance
        for (int i = 0; i < 20; i++)
        {
            Image img = new Image();
            img.Source = bmp;
            img.Stretch = Stretch.Fill;
            img.Width = 20;
            img.Height = 20;

            _images.Add(img);
            Canvas.SetTop(img, 0);
            Canvas.SetLeft(img, i * 20 + 5);
            CanvasContainer.Children.Add(img);
        }

        // Simulate some delay or any task (3 sec)
        await Task.Delay(3000); 


        Storyboard sb = new Storyboard();
        // delay animation time for each object
        TimeSpan beginTime = TimeSpan.FromMilliseconds(0);

        foreach (Image img in _images)
        {
            DoubleAnimation da = new DoubleAnimation();
            da.From = 0; // Set start value to 0 px
            da.To = 700; // Set end value to 700 px
            da.Duration = new Duration(TimeSpan.FromSeconds(2)); // Set animation time to 2 sec
            da.BeginTime = beginTime; // Set delay for each Image
            beginTime += TimeSpan.FromMilliseconds(100);

            Storyboard.SetTarget(da, img);
            Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Top)"));

            sb.Children.Add(da);
        }

        sb.Begin();
    }

Code result: 代码结果:

代码工作示范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM