仅对MOVIECLIP的可见内容进行测试

时间:2012-09-02 14:30:33

标签: actionscript-3 flash flash-cs5 flash-cs4

我使用形状屏蔽了一个Bitmap,并将其转换为影片剪辑。 现在只有蒙面图像在动画片段内。我为这个影片剪辑添加了一个鼠标单击事件监听器,然后仍然为整个影片剪辑点击。我如何设法只点击动画片段的可见区域。

`

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

`

2 个答案:

答案 0 :(得分: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处理,以便将所有内容放在相同的坐标空间中。

答案 1 :(得分: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!!!");
    }
}