0

I have been trying for a while now and I cant seem to get my collision detection working, at the moment i have it commented out due to the fact its not working.

Here are the errors I am getting when I uncomment.

Errors

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

Problem is I can not fix these errors due to lack of understanding, any help rectifying these problem will be fantastic.

Code

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);
        }
    }
}
1
  • Which line does the error occur on? Commented Feb 28, 2014 at 18:14

2 Answers 2

1
        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.
            }

        }

Try this.

EDIT: Also, if you are going to remove the apples within the loop, make sure the decrement the indexer so you don't skip an apple:

            if (collided) {
                // the apple and the pig have collided. Do something here.
                RemoveApple(myapples[indx]);
                indx --;
            }
Sign up to request clarification or add additional context in comments.

Comments

1

Here

if (DetectCollision(myapples, myPig))

you are passing a list of apples as the first parameter. However, your method

public bool DetectCollision(ContentControl ctrl1, ContentControl ctrl2)

expects a content control as the first parameter. That's what the compiler means when it complains

Argument 1: cannot convert from 'System.Collections.Generic.List' to 'System.Windows.Controls.ContentControl'

That won't work. A content control is a user interface element, whereas a List is a data structure. They are two fundamentally different things. It might be that your apples derive from content controls, in which case you would still need to pass one apple to the method instead of a whole list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.