0

我在 C# 中制作简单的游戏。我想将对象添加到画布,为它们设置动画并在点击后删除。将对象添加到画布并为其设置动画是可行的,但我无法通过点击将它们从该画布中删除。我写道:

el.Tapped += (sender, args) =>
                         {
                             LayoutRoot.Children.Remove(el);
                         };

el 是一个将被动画化的椭圆。

动画对象的方法:

private void MoveAnimation(Ellipse el, double x, double y, int time)
    {
        Duration duration = new Duration(TimeSpan.FromMilliseconds(time));

        // Create two DoubleAnimations and set their properties.
        DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
        DoubleAnimation myDoubleAnimation2 = new DoubleAnimation();

        myDoubleAnimation1.Duration = duration;
        myDoubleAnimation2.Duration = duration;

        Storyboard sb = new Storyboard();
        sb.Duration = duration;

        sb.Children.Add(myDoubleAnimation1);
        sb.Children.Add(myDoubleAnimation2);

        Storyboard.SetTarget(myDoubleAnimation1, el);
        Storyboard.SetTarget(myDoubleAnimation2, el);

        // Set the attached properties of Canvas.Left and Canvas.Top
        // to be the target properties of the two respective DoubleAnimations.
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTargetProperty(myDoubleAnimation1, "(Canvas.Left)");
        Windows.UI.Xaml.Media.Animation.Storyboard.SetTargetProperty(myDoubleAnimation2, "(Canvas.Top)");
        myDoubleAnimation1.To = x;
        myDoubleAnimation2.To = y;

        // Make the Storyboard a resource.
        LayoutRoot.Resources.Add(_animationId++ + "", sb);
        // Begin the animation.
        sb.Begin();
    }

对象处于动画状态时不会调用点击事件。如果对象在动画之后,则点击事件有效。怎么修?谢谢。

编辑:我的代码将对象添加到画布并为其设置动画:

Ellipse el = new Ellipse();
        el.Width = 50;
        el.Height = 50;
        var ib = new ImageBrush
        {
            ImageSource =
              new BitmapImage(
                new Uri("ms-appx:///Images/fly.png")
                )
        };
        el.IsTapEnabled = true;
        el.Fill = new SolidColorBrush(Colors.Blue);

        el.Tapped += (sender, args) =>
                         {
                             LayoutRoot.Children.Remove(el);
                         };

        Canvas.SetLeft(el, 0);
        Canvas.SetTop(el, 0);
        LayoutRoot.Children.Add(el);

        MoveAnimation(el, LayoutRoot.ActualWidth-400, LayoutRoot.ActualHeight-400, 5000);
4

0 回答 0