1

I am doing a collision on Windows Phone App for Windows Phone 8 using Microsoft Visual Studio 2012. I used Rect to make a rectangle border for the ball. I received this error. 'System.Windows.Rect' does not contain a definition for 'Intersects' and no extension method 'Intersects' accepting a first argument of type 'System.Windows.Rect' could be found (are you missing a using directive or an assembly reference?) The codes are as below.

var greenBallPositionX1 = Canvas.GetLeft(this.greenBall1);
var greenBallPositionY1 = Canvas.GetTop(this.greenBall1);

Rect r1 = new Rect(greenBallPositionX1, greenBallPositionY1, greenBall1.ActualWidth, greenBall1.ActualHeight);

var blueBallPositionX1 = Canvas.GetLeft(this.blueBall1);
var blueBallPositionY1 = Canvas.GetTop(this.blueBall1);

Rect r2 = new Rect(blueBallPositionX1, blueBallPositionY1, blueBall1.ActualWidth, blueBall1.ActualHeight);

if (r1.Intersects(r2))
        {
            MessageBox.Show("Collision Detected");
        }
3
  • Use intersect like r1.Intersect(r2) if no intersection exists it will return Rect.Empty. if(r1.Intersect(r2) != Rect.Empty) Jul 3, 2013 at 8:58
  • @KristerAndersson It is not detecting it. Jul 3, 2013 at 9:01
  • Looking at the documentation it should? The Intersect() method of the Rect class returns the rectangle of intersection or Rect.Empty if no intersection is found. Jul 3, 2013 at 9:04

2 Answers 2

2

your own intersects can be written as

public bool Intersects(Rect r1,Rect r2)
{
  r1.Intersect(r2);

  if(r1.IsEmpty)
  {
    return false;
  }
  else 
  {
    return true;
  }
}

then you can use

if(Intersects(r1,r2))
{
  MessageBox.Show("Collison Detected");
}
1
  • if(r1.Intersect(r2).IsEmpty) is not able to use it. Jul 3, 2013 at 9:31
0

The syntax is:

Rect.Intersect(Rect)

Lose the "s"!

http://msdn.microsoft.com/en-us/library/ms558125.aspx

1
  • Thank you. However, a new error occurred. 'Cannot implicitly convert type 'void' to 'bool' May I know how do I solve about it? Jul 3, 2013 at 9:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.