这似乎很容易,我不好意思问,但我就是不明白。
我有一个大的圆形MovieClip(被用作按钮)。这个MovieClip包含一个带有透明背景的PNG,插入到MovieClip中。由于它的大小,在4个角(边界框)上有很大的空白注册区域。
我怎样才能使鼠标寄存器在正方形边界框中仅超过圆形像素而不是空白空间(Alpha通道像素)?
简单示例代码:
public function simpleSample () : void
{
mc1.buttonMode = true;
mc1.addEventListener(MouseEvent.CLICK, doStuff);
}
public function doStuff (event:MouseEvent) : void
{
mc2.gotoAndStop(2);
}
这里有3种不同的方法来实现这一点。
EDIT由于您后来解释了您的按钮是一个图像,因此第一个选项将不适合您
-
如果
hitTestPoint
上的形状标志与您的按钮(例如它是一个形状),您可以在鼠标单击处理程序中使用hitTestPoint
来确定单击是否实际上在对象上:public function doStuff(event:MouseEvent){ //only continue if hit test point is true, //the x and y values are global (not relative to the mc your testing as one might suppose) //the third parameter should be true, so it takes into account the shape of object and not just it's bounds if(mc1.hitTestPoint(stage.mouseX, stage.mouseY, true)){ mc2.gotoAndStop(2); } }
-
如果上面的方法不起作用,因为你的按钮中有bimtap数据,那么一个简单的方法就是给按钮添加一个形状蒙版。
所以,要么在你的按钮中使用FlasPro,用圆形遮罩所有的东西,或者,当你第一次显示按钮时,通过代码执行以下操作:
var s:Shape = new Shape(); s.graphics.beginFill(0); s.graphics.drawCircle(mc1.x + (mc1.width * .5), mc1.y + (mc1.height * .5), mc1.width / 2); addChild(s); mc1.mask = s;
-
如果使用图像作为按钮,或者你想要设置一个阈值来考虑点击的透明度,那么你可以在鼠标下检查像素的透明度:
function doStuff(event:MouseEvent){ //only continue if pixel under the mosue is NOT transparent //first, you need a bitmap to work with //if you know for sure the position of your bitmap, you can do something like this: var bm:Bitmap = mc1.getChildAt(0) as Bitmap; //annoyingly though, FlashPro makes timeline bitmaps shapes, //so the above won't work UNLESS you take your bitmap in the FlashPro Library //and export it for actionscript, giving it a class name, then it will be an actual bitmap on the timeline. //As an alternative, you could (very CPU expensively) draw the whole button as a bitmap var bmd:BitmapData = new BitmapData(mc1.width,mc1.height,true,0x00000000); bmd.draw(mc1); var bm:Bitmap = new Bitmap(bmd); //we get the 32bit pixel under the mouse point var pixel:uint = bm.bitmapData.getPixel32(bm.x + event.localX,bm.y + event.localY); //then we grab just the Alpha part of that pixel ( >> 24 & 0xFF ). //if the value is 0, it's totally transparent, if it's 255, it's totally opaque. //for this example, let's say anything greater than 0 is considered good to be a click if((pixel >> 24 & 0xFF) > 0){ mc2.gotoAndStop(2); } }