开发者

Using wpf to animate ellipses consequently

开发者 https://www.devze.com 2023-03-17 04:11 出处:网络
I have to implement a road junction simple program. The junction\'s image is set as the Background Property of WPF Grid and I have ArrayLists inside a Queue to represent the color of each car, origin

I have to implement a road junction simple program. The junction's image is set as the Background Property of WPF Grid and I have ArrayLists inside a Queue to represent the color of each car, origin street and destination street.

Now, I need to animate the cars as moving ellipses and I need each car to start its movement after the privious car gets out of the screen.

I am using the following code but it only animates first car.

What is the solution?

private void button1_Click(object sender, RoutedEventArgs e)
    {

        for (int i = 0; i < queue.Count; i++)
        {

            ArrayList car = (ArrayList)queue[i];
            int id = Convert.ToInt32(car[0]);
            int color = Convert.ToInt32(car[1]);
            int from= Convert.ToInt32(car[2]);
            int to = Convert.ToInt32(car[3]);



                        Ellipse myEllipse = new Ellipse();
                        if (color == 0)
                        {
                            myEllipse.Stroke = System.Windows.Media.Brushes.Green;
                            myEllipse.Fill = System.Windows.Media.Brushes.Green;
                        }
                        else {
                            myEllipse.Stroke = System.Windows.Media.Brushes.Blue;
                        开发者_如何学JAVA    myEllipse.Fill = System.Windows.Media.Brushes.Blue;
                        }
                        myEllipse.HorizontalAlignment = HorizontalAlignment.Left;
                        myEllipse.VerticalAlignment = VerticalAlignment.Center;
                        myEllipse.Width = 45;
                        myEllipse.Height = 45;
                        myGrid.Children.Add(myEllipse);

                        DoubleAnimation da = new DoubleAnimation();
                        da.From = from;
                        da.To = to;
                        da.Duration = new Duration(TimeSpan.FromSeconds(1));
                        TranslateTransform tt = new TranslateTransform();
                        myEllipse.RenderTransform = tt;
                        tt.BeginAnimation(TranslateTransform.XProperty, da);
                         }
}


In WPF animation is is organized in other way.

I would suggest you to look at Storyboard. Hope this will help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消