views:

104

answers:

3

With Flash, is it possible to detect whether an object is fully ontop of another ? E.g. I have a rectangle (floor surface) and a circle (furniture).

Now I want to detect whether the circle is fully in (=over) the rectangle, and not just whether it hits the rectangle somewhere. Is that possible ? How ?

A: 

I've used the collision detection library seen here: http://www.tink.ws/blog/as-30-hittest/

The collision detection functions return to you a flash.geom.Rectangle object that represents the overlapping bounds of the 2 objects hitting each other. You can use it to accomplish what you want by checking the Rectangle's width and height against your circle's width and height, if they match the circle is completely over the rectangle.

walpolea
+2  A: 

Or without having to deal with new code, if your app is simple enough, you could employ a solution as illustrated by this diagram:

alt text

Having a separate hit area object that is smaller than the floor will guarantee that you'll only get a hit when the circle is entirely over the floor.

walpolea
+1. very nice :)
back2dos
A nice idea, but you'd need an extra invisible test object for every pair of objects to test. Not a general solution ;)
fenomas
+2  A: 

Sure:

function testOverlap( large:DisplayObject, small:DisplayObject ):Boolean {
    return large.getBounds(stage).containsRect( small.getBounds(stage) );
}

In other words, get the bounds rectangle of the large object, and use Rectangle.containsRect to see if it contains the bounds rectangle of the small object.

fenomas