我制作了一个Canvas(playArea),我在其上添加了1个Bee(使用playArea.children.Add(bee)
)。我还会在box
的随机点上每X秒添加一个新的playArea
。我可以使用bee
和Storyboard
将DoubleAnimation
从当前点移动到我点击的位置。
我想要做的是删除box
bee
正在传递的playAre.Children.Remove(box)
(就像它应该bee
一样。我只是无法弄明白如何检查。
这是一个例子。如果我点击A,我的EventHandler
会转到那个点并删除它上面的3个方框。我在想我应该使用自己的 //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));
}
(我不知道如何制作自己的,但这是另一个问题)。所以,我应该做什么,或者我应该做什么条件?
编辑: 以下是我使用的方法的样子。我在开始时做了什么,我只画一只蜜蜂并将它添加到我的画布上,每隔X秒我就在随机位置添加一个新盒子
curl https://somedomain.com/somepath.json
答案 0 :(得分:2)
您可以在动画上使用CurrentTimeInvalidated事件处理程序。有关详细信息,请参阅下面的代码。
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;
}
}
}