Actionscript 3 AS3-如何使用鼠标事件的像素/点检测而不是对象检测
这似乎很容易,我很不好意思问,但我就是不明白 我有一个大的圆形电影唇(用作按钮)。此MovieClip包含一个透明背景插入到MovieClip中的PNG。 由于其大小,在4个角(边界框)上有较大的空注册区域 <如何>鼠标登记为只在圆边界像素上,而不是在方框边界框中的(α通道像素)空白?< /P> 简单示例代码:Actionscript 3 AS3-如何使用鼠标事件的像素/点检测而不是对象检测,actionscript-3,Actionscript 3,这似乎很容易,我很不好意思问,但我就是不明白 我有一个大的圆形电影唇(用作按钮)。此MovieClip包含一个透明背景插入到MovieClip中的PNG。 由于其大小,在4个角(边界框)上有较大的空注册区域 鼠标登记为只在圆边界像素上,而不是在方框边界框中的(α通道像素)空白?< /P> 简单示例代码: public function simpleSample () : void { mc1.buttonMode = true; mc1.addEventListener(Mous
public function simpleSample () : void
{
mc1.buttonMode = true;
mc1.addEventListener(MouseEvent.CLICK, doStuff);
}
public function doStuff (event:MouseEvent) : void
{
mc2.gotoAndStop(2);
}
这里有三种不同的方法来实现这一点 编辑因为您稍后解释了按钮是图像,所以第一个选项对您不起作用
hitTestPoint
上的shape标志与您的按钮配合使用(例如它是一个形状),您可以在鼠标单击处理程序中使用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);
}
}
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);
}
}
我知道hitTestPoint,但不知道如何使用事件侦听器和鼠标坐标实现它……默认情况下,鼠标检测是基于像素的,而不是基于边界的。在那些“空白”区域有透明的东西吗?位图?不-只是空的/透明的空间。@Aaron,想象提问者在透明背景上有一个红色圆圈的PNG图像,如何在红色像素上获得
鼠标\u是真正的问题…@jdfinch3我看到你更新了,提到你有一个位图。一个实际的圆(向量)会起作用。您的问题有几种解决方案,但最简单的可能是使用实际的矢量圆,并将其设置为MovieClip
的hitArea
。您能描述一下您的按钮吗?这是图像吗?形状?这是一个带有透明背景的png插入到MovieClip中。因此,getgetPixel32
如果在透明像素上返回0/False
,如果在彩色像素上返回1++/True
?我可能在这里考虑过了,但我担心alpha通道具有FF
值,这会导致True
?返回值为32位颜色。如果100%透明,它将返回0
。您可以添加更多的数学运算,并接受一定范围的透明度(比如容忍高达10%的透明度等),但接下来您必须做更多的工作来解析返回的alpha部分value@VC.One-我不是位图数据方面的专家,但我处理过的每个png都报告了100%透明像素的0
。如果((getPixel32(…)>>24&0xFF)<255
,我想您可以安全地执行)。