开发者

AS3 Possible to check if mask is completely filled?

With brush_mc you can brush over a mask, which turns the pixels to transparent in brush strokes. So visually it erases the mask and the masked movieclip appears. I want to trace, if the mask is completely turned transparent.

Is it possible to check if the mask is turned completely transparent without bitmapdata?

// this creates a mask that hides the movieclip on top
var mask_mc:MovieClip = new MovieClip();
addChild(mask_mc)

//assign the mask to the movieclip it should 'cover'
mc1.mask = mask_mc;

//add event listeners for the 'brush'
brush_mc.addEventListener(MouseEvent.MOUSE_DOWN,brushDown);
brush_mc.addEventListener(MouseEvent.MOUSE_UP,brushUp);

//function to drag the brush over the mask
function brushDown(dragging:MouseEvent):void{
    dragging.currentTarget.startDrag();
    MovieClip(dragging.currentTarget).addEventListener(Event.ENTER_FRAME,erase) ;
    mask_mc.graphics.moveTo(brush_mc.x,brush_mc.y);
}

//function to stop dragging the brush over the mask
function brushUp(dragging:MouseEvent):void{
    dragging.currentTarget.stopDrag();
    MovieClip(dragging.currentTarget).removeEventListener(Event.ENTER_FRAME,erase);
}

//fill the mask with transparant pixels so the movieclip turns visible
function erase(e:Event):void{
    with(mask_mc.graphics){
        beg开发者_如何学GoinFill(0x000000);
        drawRect(brush_mc.x,brush_mc.y,brush_mc.width,brush_mc.height);
        endFill(); 
    }
}


go to here and look a tthe compare function.
What you need to do is create a second bitmapdata object the same size as your mask but with full transparent 0x00000000. Then use the compare function. As the docs say.

If the BitmapData objects are equivalent (with the same width, height, and identical pixel values), the method returns the number 0.

[EDIT]

var myTestingBitmapData:BitmapData = new BitmapData(mask_mc.width, mask_mc.height, true, 0x00000000);

// this is untested code but you might have to comvert mask_mc to bitmapdata
trace( myTestingBitmapData.compare( mask_mc) )


You could probably implement some form of counter that keeps tracks of the mask's state.

The idea would be to check the state of the mask before a brush action. You have a total number of pixels to start with, then add or substract from the counter depending on the pixel's color.

When your counter reaches a certain value, your mask is erased.

This is not an ideal solution though since your mask may look like it's been erased but you'd still have a few random pixels that keep the counter under the necessary value, so you may have to average a little... whether with the brush action or with the counter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜