鼠标仅在动画片段的不透明区域上单击检测

时间:2012-06-22 06:37:22

标签: actionscript-3 mouseevent collision-detection

我有动画片段(其中有超过1个动画片段)。我的要求是向父动画片段添加MouseEvent.CLICK事件。因此,只有在单击视觉上不透明(alpha = 100%)的区域时才会通过flash调度事件(MouseEvent.CLICK),否则忽略。

我现在所做的解决方法是创建一个alpha = 0.05圈子,它跟随Event.ENTER_FRAME中的鼠标光标,然后进行一次命中测试,接着是PixelPerfectCollisionDetection(我敬佩的一个gr8家伙)谷歌代码:))

简而言之, 我需要知道如果我只能在动画片段的视觉上不透明的区域点击并且 anywhere in the bounding box region.

时才能使Flash发送一个MouseEvent

谢谢,

Vishnu Ajit

1 个答案:

答案 0 :(得分:2)

我得到了这个解决方案。但它并不完美,因为每次都会创建bitmapData的新MovieClip对象:

myButton.addEventListener( MouseEvent.CLICK, clickFunction);

private function clickFunction(e:MouseEvent) {
    if(pixelIsVisible(e.currentTarget, e.currentTarget.mouseX, e.currentTarget.mouseY)) {
        //DoSomething
    }
}   

private function pixelIsVisible(target:*, xPos:int, yPos:int):Boolean {
    var bmp:BitmapData = new BitmapData( target.width, target.height, false );
    bmp.draw( target as MovieClip );
    var pixelValue = bmp.getPixel( xPos, yPos);

    //Dispose bitmapData to free memory
    bmp.dispose();

    var alphaValue:uint = pixelValue >> 24 & 0xFF;
    //alphaValue is from 0 to 255
    if(alphaValue >= 255) return true;
    else false;
}