这看起来应该很容易,我都不好意思问,但我就是不明白。
我有一个大的圆形 MovieClip(用作按钮)。该影片剪辑包含具有透明背景的PNG插入到影片剪辑中。
由于其尺寸,四个角(边界框)上有较大的空白注册区域。
如何让鼠标注册为仅位于圆形像素上方,而不位于方形边界框中的空白区域(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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)