【问题标题】:hit test only on the visible content of MOVIECLIP仅对 MOVIECLIP 的可见内容进行命中测试
【发布时间】:2012-08-27 12:50:28
【问题描述】:

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

`

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

`

【问题讨论】:

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


【解决方案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!!!");
    }
}

【讨论】:

    【解决方案2】:

    您不能将您的点击处理程序移动到掩码(代码中的 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 杂耍将所有东西都放在同一个坐标空间中。

    【讨论】:

    • 我无法在我的形状上获得鼠标事件。如此抢购 getpixel 方法,这为我简化了任务。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多