Question

I'm trying to check if an object is visibly "blocked" by another object at a certain xy spot. The normal hitTest method only states if two objects overlap or not, not which is visibly in front. Is there any way of achieving that, by having the xy-coordinates and two objects? So for example: 2 objects are on stage. Nr. 1 is in front and visibly covers Nr. 2 ... so an xy-hitTest with both objects should only return "true" for object nr. 1.

Thanks in advance :-)

Was it helpful?

Solution

The following function will use hitTestObject and the child indices of the objects to determine if obj1 is "in front of" obj2.

function isInFrontOf(obj1:DisplayObject, obj2:DisplayObject):Boolean{
    return obj1.hitTestObject(obj2) && (obj1.parent.getChildIndex(obj1) > obj2.parent.getChildIndex(obj2));
}

Important note: This will only work if both objects have the same parent. You absolutely can rework the function to allow for different parents, but I'll leave that up to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top