December 17, 2005
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

// 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:

That's just a few random bitmaps with alpha that get moved, scaled and rotated - when you roll over one of their opaque areas they start flickering.

Posted at December 17, 2005 04:44 PM | TrackBack (0) | Further reading
Post a comment

Email Address:



Remember info?

Thank you!

Most Visited Entries
Sketches, Works & Source Code
In Love with
Powered by
Movable Type 2.661

© Copyright Mario Klingemann

Syndicate this site:
RSS 1.0 - RSS 2.0

Quasimondo @ flickr
Quasimondo @ LinkedIn
Quasimondo @ Twitter
Quasimondo @ Facebook
Quasimondo @ MySpace
Quasimondo is a Bright
Citizen of the TRansnational Republic
My other blog in german

My family name is written Klingemann,
not Klingelmann, Klingeman, Klingaman, Kingemann,
Kindermann, Killingaman, Klingman, Klingmann, Klingonman
Klingemman, Cleangerman, Klingerman or Kleangerman

profile for Quasimondo at Stack Overflow, Q&A for professional and enthusiast programmers