如何使碰撞检测工作



我已经尝试了一段时间了,但我似乎无法让我的碰撞检测工作,目前我已将其注释掉,因为它不起作用。

这是我取消评论时遇到的错误。

错误

Argument 1: cannot convert from 'System.Collections.Generic.List<OOPigEatingApple.Apple>' to 'System.Windows.Controls.ContentControl'
The best overloaded method match for 'OOPigEatingApple.MainPage.DetectCollision(System.Windows.Controls.ContentControl, System.Windows.Controls.ContentControl)' has some invalid arguments
The best overloaded method match for 'OOPigEatingApple.MainPage.RemoveApple(OOPigEatingApple.Apple)' has some invalid arguments

问题是由于缺乏理解,我无法修复这些错误,任何帮助纠正这些问题都会很棒。

法典

namespace game
{
    public partial class MainPage : UserControl
    {
        Pig myPig;
        List<Apple> myapples;
        private int appleTimer = 0;
        //int appleCount = 0;
        public MainPage()
        {
            InitializeComponent();
            myPig = new Pig();
            myapples = new List<Apple>();
            Image myImg = new Image();
            myImg.Source = new BitmapImage(new Uri("pig3.png", UriKind.Relative));
            myImg.Width = 80;
            myImg.Height = 60;
            myPig.Content = myImg;
            LayoutRoot.Children.Add(myPig);
            Canvas.SetLeft(myPig,100);
            Canvas.SetTop(myPig, 50);
            foreach (Apple a in myapples)
            {
                LayoutRoot.Children.Add(a);
            }
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }
        public void AddApple(Apple a)
        {
            myapples.Add(a);
            LayoutRoot.Children.Add(a);
        }
        public void RemoveApple(Apple a)
        {
            myapples.Remove(a);
            LayoutRoot.Children.Remove(a);
        }
        public void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            appleTimer += 1;
            if (appleTimer > 60)
            {
                appleTimer = 0;
                AddApple(new Apple());
            }
            for (int indx = 0; indx < myapples.Count; indx++)
            {
                myapples[indx].Update(LayoutRoot);
            }
           // if (DetectCollision(myapples, myPig))
            {
             //     RemoveApple(myapples);
            }     
        }


        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Up)
                this.myPig.Move(Direction.Up);
            if (e.Key == Key.Down)
                this.myPig.Move(Direction.Down);
            if (e.Key == Key.Left)
                this.myPig.Move(Direction.Left);
            if (e.Key == Key.Right)
                this.myPig.Move(Direction.Right);
        }
        public bool DetectCollision(ContentControl ctrl1, ContentControl ctrl2)
        {
            Rect ctrl1Rect = new Rect(
                    new Point(Convert.ToDouble(ctrl1.GetValue(Canvas.LeftProperty)),
                                         Convert.ToDouble(ctrl1.GetValue(Canvas.TopProperty))),
                                 new Point((Convert.ToDouble(ctrl1.GetValue(Canvas.LeftProperty)) + ctrl1.ActualWidth),
                                         (Convert.ToDouble(ctrl1.GetValue(Canvas.TopProperty)) + ctrl1.ActualHeight))
                         );
            Rect ctrl2Rect = new Rect(
        new Point(Convert.ToDouble(ctrl2.GetValue(Canvas.LeftProperty)),
                                        Convert.ToDouble(ctrl2.GetValue(Canvas.TopProperty))),
                                new Point((Convert.ToDouble(ctrl2.GetValue(Canvas.LeftProperty)) + ctrl2.ActualWidth),
                                        (Convert.ToDouble(ctrl2.GetValue(Canvas.TopProperty)) + ctrl2.ActualHeight))
                        );
            ctrl1Rect.Intersect(ctrl2Rect);
            return !(ctrl1Rect == Rect.Empty);
        }
    }
}
        for (int indx = 0; indx < myapples.Count; indx++)
        {
            myapples[indx].Update(LayoutRoot);
            bool collided = DetectCollision(myapples[indx], myPig);
            if (collided) {
                // the apple and the pig have collided. Do something here.
            }
        }

试试这个。

编辑:另外,如果要删除循环中的苹果,请确保递减索引器,以免跳过苹果:

            if (collided) {
                // the apple and the pig have collided. Do something here.
                RemoveApple(myapples[indx]);
                indx --;
            }

这里

if (DetectCollision(myapples, myPig))

您正在传递苹果列表作为第一个参数。但是,您的方法

public bool DetectCollision(ContentControl ctrl1, ContentControl ctrl2)

需要内容控件作为第一个参数。这就是编译器抱怨时的意思

参数 1:无法从"System.Collections.Generic.List"转换为"System.Windows.Controls.ContentControl"

那行不通。内容控件是用户界面元素,而列表是数据结构。它们是两个根本不同的东西。可能是您的苹果派生自内容控件,在这种情况下,您仍然需要将一个苹果传递给该方法而不是整个列表

最新更新