Flash 8: Hitting Bitmaps with the concatenatedMatrix

I always wondered when I would find a use for MovieClip.transform.concatenatedMatrix - finally I’ve found one. It becomes very useful when you try to detect if you’ve hit a BitmapData object under your mouse pointer - especially if that bitmap is nested inside one or more movieclips that have been scaled or rotated. Think of it like the localToGlobal for bitmaps - just a little bit more elegant.

Here’s a code to do that:

var m:Matrix = mc.transform.concatenatedMatrix;
// mc is the clip that holds the bitmapData Object

m.invert();
// that’s the trick - by inverting the matrix
// we can map the mouse coordinates back into the mc

var mp:Point = new Point( _root._xmouse, _root._ymouse );
// the current mouse coordinates as a Point
// note _root is not evil here, because that’s
// the same level as concatenatedMatrix points to

var p:Point = m.transformPoint( mp );
// transform the mouse coordinates
// through the inverted matrix

if ( myBitmap.hitTest( new Point(), 8, p ) ){
// and make the hit test
// 8 in this case is the minimum alpha level
//to count as a hit

trace(”bitmap hit”);
}

And here is a demo: