2009-08-04 8 views
2

私はゲームをデザインしています。このWPF - ストーリーボード完成イベント

class Anima 
{ 
    Storyboard story; 
    Random rand; 
    Canvas canvas; 
    Ellipse target; 

    public Anima() { 
     rand = new Random(); 
     canvas = new Canvas(); 
     target = new Ellipse(); 

     target.Fill = Brushes.Red; 
     target.Width = 50; 
     target.Height = 50; 
     Canvas.SetLeft(target, rand.NextDouble() * 300); 
     Canvas.SetTop(target, rand.NextDouble() * 300); 
     canvas.Children.Add(target); 

     story = new Storyboard(); 
     story.BeginTime = TimeSpan.FromMilliseconds(rand.Next(500, 5000)); 

     DoubleAnimation a = new DoubleAnimation(); 
     a.To = rand.NextDouble() * 300; 
     a.Duration = new Duration(TimeSpan.FromMilliseconds(50)); 

     Storyboard.SetTarget(a, target); 
     Storyboard.SetTargetProperty(a, new PropertyPath(Canvas.LeftProperty)); 
     story.Children.Add(a); 

     DoubleAnimation b = new DoubleAnimation(); 
     b.To = rand.NextDouble() * 300; 
     b.Duration = new Duration(TimeSpan.FromMilliseconds(50)); 

     Storyboard.SetTarget(b, target); 
     Storyboard.SetTargetProperty(b, new PropertyPath(Canvas.TopProperty)); 
     story.Children.Add(b); 

     story.Completed += new EventHandler(story_Completed); 

     Window win = new Window(); 

     win.Loaded += delegate(object sender, RoutedEventArgs e) { 
      story.Begin(); 
     }; 

     win.Content = canvas; 
     win.Show(); 
    } 

    void story_Completed(object sender, EventArgs e) { 
     int next = rand.Next(500, 5000); 
     double left = rand.NextDouble() * 300; 
     double top = rand.NextDouble() * 300; 

     Console.WriteLine("position: ({0:G6}, {1:G6})", Canvas.GetLeft(target), Canvas.GetTop(target)); 
     Console.WriteLine("state : wait for " + next + " ms"); 
     Console.WriteLine("next : ({0:G6}, {1:G6})", left, top); 
     Console.WriteLine(); 

     (story.Children[0] as DoubleAnimation).To = left; 
     (story.Children[1] as DoubleAnimation).To = top; 

     story.BeginTime = TimeSpan.FromMilliseconds(next); 
     story.Begin(); 
    } 
} 

などのすべてが素晴らしいですが、私は楕円は右の位置を取得し、2%〜50%程度の誤差をしなかったことがわかりました。アニメーションが終了する前にStoryboard.Completedイベントが発生したようです。

どうしたのですか?

答えて

0

使用Dispatcher.BeginInvoke(...)を始めるだろういくつかのリンクです、あなたは正しい値

を得ることができます
関連する問題