AS3 Pixel Precise Hit Testing
This is a simple utility function to detect pixel precise hit testing. Here i use the features of the BitmapData class to make a pixel perfect hitText
public static function hitTest(object:DisplayObject, point:Point):Boolean
{
if(object is BitmapData)
{
return (object as BitmapData).hitTest(new Point(0,0), 0, object.globalToLocal(point));
}
else
{
if(!object.hitTestPoint(point.x, point.y, true))
{
return false;
}
else
{
var bmapData:BitmapData = new BitmapData(object.width, object.height, true, 0x00000000);
bmapData.draw(object, new Matrix());
var returnVal:Boolean = bmapData.hitTest(new Point(0,0), 0, object.globalToLocal(point));
bmapData.dispose();
return returnVal;
}
}
}
You can implement on your code like this:
this.addEventListener(Event.Enter_FRAME, hitTesting);
private function hitTesting(e:Event):void
{
DisplayObjectUtils.hitTest(myTarget, new Point(this.mouseX, this.mouseY));
// DisplayObjectUtils is a class where i store all my display object related methods. you can create your own
}