I have a flash movie in which I want to allow the user to either erase a layer (using their mouse) to reveal the layer underneath, or to have the mouse draw a mask, to reveal the layer underneath.
Is one method easier than the other? Any good samples out there that will show how to simply do that?
I don't think there's anyway to dynamically erase in actionscript, but
creating a mask which reveals the layer underneath is doable. Here's how.
Let's say you've already created a mask movie clip called myMask.
Basically you would setup the mask movie to listen to mouse events:
Code:
Mouse.addListener(myMask);
then add some functions to get the mask
to draw at the current mouse position
when the mouse is moved, and the button is down...
Code:
myMask.onMouseDown = function()
{
this.isErasing = true;
}
myMask.onMouseDown = function()
{
this.isErasing = false;
}
myMask.onMouseMove = function()
{
if (! this.isErasing)
return;
var mx = this._xmouse;
var my = this._ymouse;
// Draw something at mx,my.
// for a mask it must be a fill,
// not just lines.
// it will look nicer if you draw a circle using curveTo, but will also be slower...
var eWidth = 10; // radius of eraser
this.moveTo(mx-eWidth,my-eWidth);
this.beginFill(0,100);
this.lineTo(mx+eWidth,my-eWidth);
this.lineTo(mx+eWidth,my+eWidth);
this.lineTo(mx-eWidth,my+eWidth);
this.lineTo(mx-eWidth,my-eWidth);
this.endFill();
}
The PROBLEM with this method is that the more drawing you do, the slower your movie is going to get, because all those overlapping boxes you are drawing suck CPU, until your mask does a this.clear(); (which erases the boxes).
So it would probably be good to spend some time finding a way to optimize your eraser to draw fewer boxes...