0

大家好,基本上这是僵尸行走的代码,现在我想做的是当我点击他们消失的僵尸时,我该怎么做?或者如果僵尸在与另一张图像碰撞时会消失,我该怎么办?就像子弹击中他们一样。

public class Gaston {
        public double X { get; set; }
        public double Y { get; set; }
        public Image Image { get; set; }
        public int Colspan { get; set; }
        public int Rowspan { get; set; }
    }
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
         DispatcherTimer dispatcherTimer;
         List<Gaston> Gastons;
         int timesTicked = 0;
         int timesToTick = 10;
         Image zombieImg;
         public MainPage()
        {
            this.InitializeComponent();
            Gastons = new List<Gaston>();
                DispatcherTimerSetup();
                Zombie();

        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Zombie()
        {
            Gaston newGaston = new Gaston
            {
                Colspan = -10,
                Image = new Image(),
                Rowspan = -10,
                X = -10,
                Y = -10
            };
            BitmapImage bmp;
            bmp = new BitmapImage(new Uri (this.BaseUri, "/Assets/zombie.png"));
            newGaston.Image.Source = bmp;
            Gastons.Add(newGaston);    

            Grid.Children.Add(newGaston.Image);

        }
        private void ZombieWalks()
        {
            foreach (Gaston me in Gastons)
            {
                me.Image.Margin = new Thickness(me.Image.Margin.Left - 10, me.Image.Margin.Bottom - 10, me.Image.Margin.Right + 10, me.Image.Margin.Top + 10);
           }

        }




        public void DispatcherTimerSetup()
        {
            dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += dispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }

        void dispatcherTimer_Tick(object sender, object e)
        {

            AddZombie();
            timesTicked++;
            ZombieWalks();
            if (timesTicked > timesToTick)
            {

            }

        }
        private void AddZombie()
        {
            if (timesTicked == 5)
            {
                    Zombie();
                DispatcherTimerSetup();
            }
        }        
    }
}
4

1 回答 1

1

将 Tapped 事件挂在您的图像上(并将 IsTapEnabled 设置为 true)。然后,在这种情况下,移除僵尸。

BitmapImage bmp;
bmp = new BitmapImage(new Uri (this.BaseUri, "/Assets/zombie.png"));
newGaston.Image.Source = bmp;
newGaston.Image.Tapped += GastonTapped;

....
private void GastonTapped(object sender, TappedRoutedEventArgs e)
{
// remove the zombie.
}

另外 - 我强烈建议您使用 Canvas 来定位您的元素。容易得多。

于 2013-10-29T07:13:43.283 回答