0

What is quick way to check collision between 2 rectangles then you have rectangle coordinates like:

Red rectangle: A1(x1, y1), B1(x2, y2).

Blue rectangle: A2(x3, y3), B2(x4, y4).

enter image description here

Community
  • 1
  • 1
wyy
  • 515
  • 2
  • 4
  • 18
  • This post should help http://stackoverflow.com/questions/115426/algorithm-to-detect-intersection-of-two-rectangles – Pan Jan 10 '14 at 16:25

2 Answers2

5

Do you check the Rectangle class Link to AS3 Doc?

intersection(toIntersect:Rectangle):Rectangle

If the Rectangle object specified in the toIntersect parameter intersects with this Rectangle object, returns the area of intersection as a Rectangle object.

intersects(toIntersect:Rectangle):Boolean

Determines whether the object specified in the toIntersect parameter intersects with this Rectangle object.

Community
  • 1
  • 1
Nambew
  • 640
  • 5
  • 8
2

hitTestObject is your hero.

// Create Box1
var box1:Sprite = new Sprite(); 
box1.graphics.beginFill(0x0000FF);
box1.graphics.drawRect(0, 0, x2-x1, y2-y1);
box1.graphics.endFill();
box1.x = x1;
box1.y = y1;
addChild(box1);

// Create Box2
var box2:Sprite = new Sprite(); 
box2.graphics.beginFill(0x0000FF);
box2.graphics.drawRect(0, 0, x4-x3, y4-y3);
box2.graphics.endFill();
box2.x = x3;
box2.y = y3;
addChild(box2);


//Now test if collide
var _collide:Boolean = box1.hitTestObject(box2);
Doppio
  • 2,018
  • 12
  • 11