1

我已经使用形状掩盖了位图并将其转换为影片剪辑。现在只有蒙版的图像在影片剪辑内。我已经为这个影片剪辑添加了一个鼠标单击事件侦听器,并且仍然对整个影片剪辑进行了单击。我如何设法仅单击影片剪辑的可见区域。

`

//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");
}

`

4

2 回答 2

1

您不能将您的点击处理程序移动到掩码(代码中的 s1)吗?

这应该只允许在可见圆圈内单击:

import flash.events.MouseEvent;
import flash.display.Sprite;

var shape:Sprite = addChild(new Sprite()) as Sprite;
shape.graphics.beginFill(0xFF0000, 1);
shape.graphics.drawRect(0,0,200,200);
shape.x = shape.y = 50;

var masque:Sprite = shape.addChild(new Sprite()) as Sprite;
masque.graphics.beginFill(0x00FF00, 1);
masque.graphics.drawCircle(100,100,100);
masque.buttonMode = masque.useHandCursor = true;
shape.mask = masque;

function handleMasqueClick($e:MouseEvent):void
{
    trace("CLICK");
}

masque.addEventListener(MouseEvent.CLICK, handleMasqueClick);

不确定我是否理解 - 这就是你想要的吗?

否则,如果您的意思是要避免在蒙版区域内对位图的透明部分产生正面影响,则需要获取鼠标在蒙版上单击的位置并针对位图本身对该点进行测试(请参阅: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#hitTest%28%29 ) - 可能需要一些localToGlobal/GlobalToLocal杂耍来获取所有内容同坐标空间。

于 2012-09-02T17:37:56.823 回答
1

我使用“获取像素”的技巧让它工作。鼠标点击只在彩色像素上而不是白色像素上。

    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!!!");
    }
}
于 2012-09-03T02:48:39.113 回答