Search code examples
actionscript-3flashflash-cs5flash-cs4

hit test only on the visible content of MOVIECLIP


I have masked a Bitmap using a shape and have converted this to a movie clip. Now only the masked image is inside the movieclip. i have added a mouse click event listener for this movie clip and still the click is taken for the whole movieclip. how do i manage to click only the visible area of the movieclip.

`

//img is a bitmap on stage and s1 is an irregular shape on the stage which is a movieclip
img.mask = s1;

var bmp:Bitmap = new Bitmap(new BitmapData(img.width,img.height,true));
bmp.bitmapData.draw(img);
var n:MovieClip = new MovieClip();



n.addChild(bmp);

addChild(n);


trace(s1.width + " " + s1.height);
trace(n.width + " " + n.height);
n.addEventListener(MouseEvent.CLICK, clicked);
removeChild(s1);
removeChild(img);

function clicked(m:MouseEvent):void
{
            trace(n.hitTestPoint(n.mouseX,n.mouseY, false));
            trace("clikckedek kkc kkeke");
}

`


Solution

  • I got it working using the trick of "get pixel". the mouse click is taken only on the colored pixels and not white pixel.

        function clickHandler (event:MouseEvent):void
    {
        //check if the bitmap inside the object that was clicked has a transparent pixel at the current mouse position
        var mcAlpha:String = (event.currentTarget.getChildAt(0).bitmapData.getPixel32(event.localX,event.localY) >> 24 & 0xFF).toString(16);
    
        if (mcAlpha == "0")
        {
            trace ("transparent!");
        }
        else
        {
            trace ("CLICK!!!");
        }
    }