Tutorial – ActionScript 3.0 Masks
Flash Tutorial March 19th, 2008I recently had a request for a tutorial on how to apply masks using ActionScript 3.0. If you've ever tried doing this with AS 3, then you've probably discovered that it doesn't work the same as it did in AS 2.
So, for those of you who have never used masks, or for those who have never used ActionScript to create masks, let's start with the basics.
First of all, what is a mask? Simply stated, a mask is a shape that is used to reveal portions of another asset on the stage. When a shape is applied as a mask to a Movie Clip, for example, the only portion of that Movie Clip that remains visible is the portion of the Movie Clip that is touching the mask.
If you want to apply a mask to an object without using ActionScript, the shape that will serve as the mask needs to be situated in a layer directly above the layer that contains the object being masked, and then the layer with the mask on it is simply converted to a mask layer by right-clicking on it and clicking on "Mask."
However, in ActionScript, it works a little differently. In ActionScript, it doesn't really matter what layers your objects are in. Even if your mask shape is in the same layer as the object to be masked, the aforementioned mask can still be applied.
For this tutorial, we're going to create a spotlight mask effect. We're basically going to create a circle that follows the mouse cursor around on the stage, and this circle is going to be applied as a mask to an image that we're going to store as a Movie Clip Symbol.
1. Import the Image. Cmd+R (or Ctrl+R for the PC) will import an object to the stage. Choose any image you'd like. The picture I'll be using is an image of my late English Bulldog, Bane.
2. Convert the Image to a Movie Clip Symbol and give it an Instance Name. For this tutorial, I'll give the Movie Clip an instance name of "bane_mc".

3. Create your mask shape. As I mentioned before, this shape can be in the same layer as your image, but it will be easier to see and manage if you put it in a new layer above the image layer. For this tutorial, I'm going to create a simple circle with edges that fade to transparent. I'll do this by applying a radial gradient that fades from solid green in the center to transparent green on the edges. Here's what my gradient settings look like:

Notice that the right handle of the gradient (which represents the outer color of the radial gradient) is actually set to the same color as the left (inner) handle, but the "Alpha" is simply dragged down to 0%, making it transparent around the edges. Also notice that the left handle has been dragged over so that it's close to the right handle. This will result in a solid center with only the very edges fading out to transparency.
It's important to point out at this point, in case you don't already know, that the ONLY way to create a gradient mask with different levels of transparency is to apply the mask using ActionScript. If you try to apply the mask using layers, it will simply show up as a solid circle.
Note: Keep in mind that the color of your mask does not matter. It will never actually be seen anyways!
Here's the shape I drew for the mask:

4. Convert the oval to a Movie Clip Symbol and give it an instance name. When you convert it, make sure you put the registration point in the center of the shape, as shown here (simply click in the center square to make this change):

The reason we want the registration point centered is because we're going to tie this movie clip to the location of the mouse cursor, and we want to tie the CENTER of the movie clip to the cursor, NOT the top left corner of the movie clip.
For this example, I'm going to give our mask movie clip an instance name of "mask_mc."
5. Tie the "mask_mc" movie to the location of the cursor. This is not something we're just going to do once. We need to make sure that the mask movie clip is ALWAYS tied to the location of the cursor, so we're going to create an ENTER_FRAME event in ActionScript that will constantly be checking the location of the cursor and moving the mask movie clip to that location. So we need to create a new layer, name the new layer "Actions" or "A" or whatever you want, and then select frame 1 of the actions layer and hit Option+F9 (or just F9 on PC) to open up the Actions Panel for that frame. Here's the code that will tie the circle to the cursor:

Line 1 creates the ENTER_FRAME event that we discussed a moment ago. Since my Flash file is set to the default of 12 frames per second, this ENTER_FRAME event will trigger the "moveMask" function to run 12 times per second. So, 12 times per second, we're relocating the mask_mc movie clip to wherever the cursor is at that moment in time.
If you test your movie now, you should see a bright green circle following your mouse around on the stage.
6. Apply the circle as a mask to the image Movie Clip. In ActionScript 2.0, you would have done this using the following line of code:
mask_mc.mask(bane_mc);
(Remember, "bane_mc" is the instance name of the Movie Clip that contains our image.)
However, things are done a little bit differently in ActionScript 3.0. There is no longer a method of the Movie Clip class called "mask()". Rather, "mask" is now a property, and you can set this property equal to another Movie Clip. In order for this to work properly, we need to apply this property to the object that is being masked, and we'll set the property equal to the instance name of the Movie Clip that will serve as the mask. In other words, you simply need to add the following code to your Actions:
bane_mc.mask = mask_mc;
I added this code to the very top of my Actions, on line 1.
If you test your movie now, you'll find the effect we've been going for . . . except that something is missing. What about the fuzzy edges we put around the circle? What about the transparency?
Well, Flash will only recognize this transparency if we tell it to cache the mask and image Movie Clips as bitmaps. And how do we do this? Simple:

After adding lines 2 and 3 to our Actions, we come away with our final result:
Here is the FLA file for your reference: as_mask.zip


March 22nd, 2008 at 1:43 pm
very good interesting stuff.
]
Thanks
April 10th, 2008 at 4:33 pm
Hi!
I’ve been working with a mask in my project and I was wondering if it’s possible to create inverse mask? So that everything else is displayed except the shape area. Would be useful to my game.
In that case of your tutorial, it would be possible to place and black layer with .5 alpha over the dog picture and then the shaped area would erase the dark layer, from the spot.
If you get what I mean..?
April 25th, 2008 at 2:55 am
henri-
I don’t know if you are still checking this post, but if I understand you correctly, I’m trying to do a similar thing as you in a game of my own right now. I have a movie clip which I want to appear black (or you could have it not appear at all) until it walks through a certain area where it will be seen until it passes out of that area.
If you don’t want it to appear at all except within the mask shape, then in your actionscript just create the clip, and create the maks clip (i use a small rectangle), and assign the mask clip to the “.mask” property of your first clip, and add them both to the stage. You should see everything else that has been added to the stage, but you’ll only see the clip you’re masking if it passes over the area of your mask clip. My only problem is that you can’t assign the same mask clip to multiple movie clips…
June 3rd, 2008 at 11:36 pm
hello sir, that is really a nice example… i have one question…. is there is any way in AS3 that we can know, which layer is mask or not…. suppose user give us a swf we loaded it in another swf. so from there can we find which layer is mask or not????? bit difficult.. hope u solve it soon. TC
July 25th, 2008 at 1:23 pm
Thanks! This was just was I was looking for. I appreciate how you explained the AS3 way of doing this along with what has changed since AS2 and why.
September 3rd, 2008 at 1:47 pm
Hi! I’m just learning flash, so I’m a bit of a newbie. How did you cause the screen to be black instead of white? The best I’ve got so far is to set my picture_mc as having opaque background black. I’d rather set the stage like this, and when I do this (I’m not sure why) my circle isn’t nice and transparent at the edges anymore
Any thoughts?
My code is below:
mask_mc.addEventListener(Event.ENTER_FRAME, movemask)
dress_mc.mask = mask_mc
dress_mc.opaqueBackground = 0×000000
dress_mc.cacheAsBitmap = true
mask_mc.cacheAsBitmap = true
function movemask(e:Event):void
{
mask_mc.x = mouseX
mask_mc.y = mouseY
}
January 8th, 2009 at 11:41 am
Thanks god!!!
This cacheAsBitmap property is real crap!
I’ve been stuck for hours .
Thank you very much
January 19th, 2009 at 3:54 pm
this is very handy
working on a project at college, thanks for this post, lifesaver ^^
February 18th, 2009 at 8:26 pm
Thank you, Craig… best explanation i found… helps a lot!
April 28th, 2009 at 5:22 am
total rip-off i found:
http://www.pqdvd.com/blog/air/?p=82
August 14th, 2009 at 11:33 am
Who ripped who off? Check the dates, V, this was first.
March 14th, 2010 at 2:52 am
You have saved me. I have been looking for this informaton all night. It works now. Thank you.