Search code examples
flashactionscript-3maskfillerase

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){
        beginFill(0x000000);
        drawRect(brush_mc.x,brush_mc.y,brush_mc.width,brush_mc.height);
        endFill(); 
    }
}

Solution

  • 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) )