0

I need to detect when MC2 is over MC1 that it is inside MC1's borders. to do this I would usually use 4 separate if x y constraints, and unfortunately .hitTestObject in my creations also seem to need 4 separate if x y + - constraints.

Does anyone know a more simplistic way to achieve this.

or is x y + - constraints still the only way to do this?

Thank you in advance.

enter image description here

Sean Carroll
  • 107
  • 1
  • 14
  • [How about hitTestPoint](https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#hitTestPoint())? –  Feb 21 '18 at 12:52

2 Answers2

1

The final solution for your problem to detect hit of two shapes, is to use bitmapData.hitTest(). you can detect hit between any shapes and not only Rectangles. for that, you have to draw both of your shapes on bitmapData like line belo:

var shape1Bitmap:BitmapData = new BitmapData(shape1MC.with,shape1MC.height,true,0x000000);
shape1Bitmap.draw(shape1MC);

var shape2Bitmap:BitmapData = new BitmapData(shape1MC.with,shape1MC.height,true,0x000000);
shape1Bitmap.draw(shape1MC);

shape1Bitmap.hitTest(new Point(),shape2Bitmap):Boolean;******

to continue usint BitmapData.hitTest(), folow the orders here : https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#hitTest()

http://dougmccune.com/blog/2007/02/03/using-hittestpoint-or-hittest-on-transparent-png-images/

It is a little complicated to add the bitmapData.hitTest() samples here. if any further questions left, please let me know to explain.

Good luck

MESepehr
  • 758
  • 6
  • 19
  • I don't think you're understanding the question. This answer, while a good alternative for bitmaps with transparent pixels, will produce the same result as `hitTestObject`, in that it will return `true` for any overlap, where as the question is about determining if the square is completely inside the circle. – BadFeelingAboutThis Feb 26 '18 at 16:57
0

I don't know of a built in way to do this, but it's easy enough using hitTestPoint with each corner of the square:

function isSquareInsideObject(square:DisplayObject, obj:DisplayObject):Boolean {
    if(!obj.hitTestPoint(square.x, square.y, true)) return false;
    if(!obj.hitTestPoint(square.x + square.width, square.y, true)) return false;
    if(!obj.hitTestPoint(square.x + square.width, square.y + square.height, true)) return false;
    if(!obj.hitTestPoint(square.x, square.y + square.height, true)) return false;
    return true;
}

For more complex shapes than a square, you'd have to add more points to be accurate and it becomes a less elegant and less performant solution then.

You need that shape argument (third parameter for hitTestPoint) set to true if you want to test against the actual circle shape instead of the rectangular bounding box of the circle. If your circle is a bitmap (and not a shape), then I'd suggest putting a circular mask on the object to achieve the same result.

If your square isn't anchored at 0,0, or you don't mind the extra (small) performance hit, you could also use var bounds:Rectangle = square.getBounds(this) and then use the convenience properties of the rectangle object (bounds.bottomLeft, bottomRight, topLeft, topRight)

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40