2016-11-23 4 views
1

Ich habe einen Canvas (playArea) erstellt, auf dem ich 1 Bee hinzugefügt habe (unter Verwendung von playArea.children.Add(bee)). Und ich füge auch neue box alle X Sekunden an zufälligen Punkten auf dem playArea hinzu. Ich bin in der Lage, meine bee von dem aktuellen Punkt zu dem Punkt, den ich klicke, mit Storyboard und DoubleAnimation zu verschieben.C# Lösche UIElement, wenn ein anderes UIElement ihn passiert

Was ich tun möchte, ist ich die box löschen möchten, die bee durchläuft (wie es nur playAre.Children.Remove(box) tun sollte. Ich kann es einfach nicht herausfinden, wie es zu überprüfen.

example

Hier ist ein Beispiel: Wenn ich auf A klicke, würde mein bee zu diesem Punkt gehen und die 3 Kästchen auf dem Weg entfernen.Ich dachte, ich sollte wahrscheinlich meine eigene EventHandler verwenden (ich bin mir nicht sicher, wie ich meine eigene machen soll Aber das ist ein anderes Problem.) Also, was soll ich tun, oder welche Bedingung sollte ich machen?

EDIT: So sieht meine Methoden aus wie ich. Was ich an der beggining tun, ziehe ich nur eine Biene und fügen Sie ihn in meiner Leinwand, und alle X Sekunden ich nur hinzufügen, eine neue Box an zufälligen Ort

//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

welchem ​​Rahmen ist das? Einheit? Das Aktualisieren des Titels oder der Tags bringt mehr sachkundige Personen. – akatakritos

+0

Es ist .NET, aktualisiert es – Tripper

+1

Sie müssen die Position Ihrer Biene und Ihre Box (verwenden Sie Canvas.GetLeft, Canvas.GetTop Methoden dafür) und seine Dimensionen. Dann ist es ein wenig einfache Mathematik, um zu überprüfen, ob die Biene durch die Box gegangen ist. Sie müssen dies für alle Boxen tun (Sie können die Sammlung von ihnen erhalten wie diese playArea.children.OfType ()). Aber es ist schwer zu helfen, es sei denn du stellst einen Code zur Verfügung, mit dem du arbeitest – Omilis

Antwort

2

Sie auf Ihre Animationen die CurrentTimeInvalidated Ereignishandler verwenden könnte. Siehe Code unten für Details.

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; 
     } 
    } 
} 
Verwandte Themen