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 det开发者_JAVA百科ect whether the circle is fully in (=over) the rectangle, and not just whether it hits the rectangle somewhere. Is that possible ? How ?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.
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 http://www.andrewwalpole.com/hitsolution.gif
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.
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.
精彩评论