wyy
wyy

Reputation: 545

Check collision between rectangles AS3

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

Upvotes: 0

Views: 1299

Answers (2)

Doppio
Doppio

Reputation: 2188

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);

Upvotes: 2

Nambew
Nambew

Reputation: 640

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.

Upvotes: 5

Related Questions