2016-11-23 55 views
1

我製作了一個Canvas(playArea),我在其上添加了1個Bee(使用playArea.children.Add(bee))。並且我還在playArea的隨機點每X秒添加新的box。我可以將我的bee從當前點移動到點擊使用StoryboardDoubleAnimationC#每當另一個UIElement通過他時刪除UIElement

我想要做什麼是我想要刪除的boxbee是通過(像它應該只是做playAre.Children.Remove(box)。我只是無法弄清楚如何去檢查一下。

example

下面是一個例子,如果我點擊A,我的bee會去那個地方,並且移除3個盒子,我想我應該用我自己的EventHandler(我不知道如何讓我自己做,但這是另一個問題)所以任何ide應該怎麼做,或者我應該做什麼條件?

編輯: 以下是我使用的方法的樣子。我在beggining做什麼,我只是畫一個蜜蜂,並把它添加到我的畫布,而每X秒我只是在隨機位置

//moving the bee to the position which I got from Point p = Mouse.GetPosition(playArea); 
    public static void MoveBee(UIElement element, double toX, double toY) 
    { 
     double fromX = Canvas.GetLeft(element); 
     double fromY = Canvas.GetTop(element); 

     Storyboard storyboard = new Storyboard(); 
     DoubleAnimation animationX = CreateDoubleAnimation(element, 
      fromX, toX, new PropertyPath(Canvas.LeftProperty)); 
     DoubleAnimation animationY = CreateDoubleAnimation(element, 
      fromY, toY, new PropertyPath(Canvas.TopProperty)); 
     storyboard.Children.Add(animationX); 
     storyboard.Children.Add(animationY); 
     storyboard.Begin(); 
    } 

    public static DoubleAnimation CreateDoubleAnimation(UIElement element, 
     double from, double to, PropertyPath propertyToAnimate) 
    { 
     DoubleAnimation animation = new DoubleAnimation(); 
     Storyboard.SetTarget(animation, element); 
     Storyboard.SetTargetProperty(animation, propertyToAnimate); 
     animation.From = from; 
     animation.To = to; 
     animation.Duration = TimeSpan.FromSeconds(3); 
     return animation; 
    } 

    public void DrawBox() 
    { 
     BoxControl newBox = new BoxControl(); 
     playArea.Children.Add(newBox); 
     Canvas.SetTop(newBox, random.Next(0, 419)); 
     Canvas.SetLeft(newBox, random.Next(0, 792)); 
    } 
+1

什麼框架,這是什麼?統一?更新標題或標籤會吸引更多有見識的人。 – akatakritos

+0

它是.NET,更新它 – Tripper

+1

你需要知道你的蜜蜂和你的盒子的位置(使用Canvas.GetLeft,Canvas.GetTop方法)及其尺寸。然後,檢查蜜蜂是否通過盒子,這是一個簡單的數學。你需要爲所有盒子做這件事(你可以像這樣playArea.children.OfType ())獲得它們的集合。但除非您提供與您一起工作的實際代碼,否則很難提供幫助 – Omilis

回答

2

添加一個新的對話框,您可以使用CurrentTimeInvalidated事件處理程序上的動畫。詳情請參閱下面的代碼。

Valid XHTML


public partial class MainWindow : Window 
{ 
    public Random Rng { get; set; } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     Rng = new Random(0); 

     do 
     { 
      AddBoxToRandomPointInPlayArea(); 
     } while (PlayArea.Children.Count < 10); 

    } 

    private void AddBoxToRandomPointInPlayArea() 
    { 
     var x = Rng.Next(0, Convert.ToInt32(PlayArea.Width)); 
     var y = Rng.Next(0, Convert.ToInt32(PlayArea.Height)); 

     var box = new Rectangle 
     { 
      Height = 10, 
      Width = 30, 
      Fill = Brushes.Brown 
     }; 

     PlayArea.Children.Add(box); 

     Canvas.SetLeft(box, x); 
     Canvas.SetTop(box, y); 
    } 

    private void PlayArea_MouseDown(object sender, MouseButtonEventArgs e) 
    { 
     var x = Canvas.GetLeft(Bee); 
     var y = Canvas.GetTop(Bee); 

     var storyboard = new Storyboard(); 

     var xTranslation = new DoubleAnimation(x, e.GetPosition(PlayArea).X, new Duration(new TimeSpan(0, 0, 5))); 
     Storyboard.SetTargetProperty(xTranslation, new PropertyPath(Canvas.LeftProperty)); 
     xTranslation.CurrentTimeInvalidated += CheckForCollidingBoxesAndRemoveIfNeeded; 

     storyboard.Children.Add(xTranslation); 

     var yTranslation = new DoubleAnimation(y, e.GetPosition(PlayArea).Y, new Duration(new TimeSpan(0, 0, 5))); 
     Storyboard.SetTargetProperty(yTranslation, new PropertyPath(Canvas.TopProperty)); 
     yTranslation.CurrentTimeInvalidated += CheckForCollidingBoxesAndRemoveIfNeeded; 

     storyboard.Children.Add(yTranslation); 

     Bee.BeginStoryboard(storyboard); 
    } 


    private void CheckForCollidingBoxesAndRemoveIfNeeded(object sender, EventArgs eventArgs) 
    { 
     var idxToRemoveAt = GetIndexOfBoxCollidingWithBee; 
     if (idxToRemoveAt != -1) 
     { 
      PlayArea.Children.RemoveAt(idxToRemoveAt); 
     } 
    } 

    /// <summary> 
    /// returns 0 if no boxes collide, otherwise the number is the index of the box in the 
    /// </summary> 
    public int GetIndexOfBoxCollidingWithBee 
    { 
     get 
     { 
      var beeTopLeft = PointToScreen(new Point(Canvas.GetLeft(Bee), Canvas.GetTop(Bee))); // local to world coordinates 
      var beeCentroid = new Point((beeTopLeft.X + Bee.ActualWidth) * 0.5, (beeTopLeft.Y + Bee.ActualHeight) * 0.5); // center point of bee 

      for (var idx = 0; idx < PlayArea.Children.Count; idx++) 
      { 
       var child = PlayArea.Children[idx]; 
       var currentBoxInSearch = child as Rectangle; 
       if (currentBoxInSearch != null) 
       { 
        var boxTopLeft = PointToScreen(new Point(Canvas.GetLeft(currentBoxInSearch), Canvas.GetTop(currentBoxInSearch))); // local to world coordinates 
        var boxCentroid = new Point((boxTopLeft.X + currentBoxInSearch.ActualWidth) * 0.5, (boxTopLeft.Y + currentBoxInSearch.ActualHeight) * 0.5); // center point of bee 

        var xCollided = false; 
        var yCollided = false; 

        if (Math.Abs(beeCentroid.X - boxCentroid.X) < Bee.ActualWidth*0.5) 
         xCollided = true; 

        if (Math.Abs(beeCentroid.Y - boxCentroid.Y) < Bee.ActualHeight*0.5) 
         yCollided = true; 

        if (xCollided && yCollided) 
         return idx; 
       } 
      } 

      return -1; 
     } 
    } 
}