我必须实施一个路口简单程序。交汇点的图像被设置为WPF网格的背景属性,我在队列中有阵列列表来表示每辆车,原始街道和目的地街道的颜色。
现在,我需要将汽车设置为移动椭圆的动画,我需要每辆汽车在私人汽车离开屏幕后开始运动。
我使用以下代码,但它只能动画第一辆车
解决方案是什么?
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;
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);
}
}