8 August 2011
This guide assumes you are familiar with the Flash Professional workspace and have a basic knowledge of working with FLA files and ActionScript.
Intermediate
A mask is a graphic that is used to crop the visual display of another graphic. You can use masks in Adobe Flash Professional CS5 to create animated special effects such as spotlights or transitions, or to crop the viewable area of an image.
Masks contain at least two parts: the graphic you want to mask and the graphic used to mask it. The graphic applied as a mask acts as a definition for an outline through which other content can be seen—similar to the way a window masks the view from outside. Similar to a window, a mask graphic appears invisible at runtime.
You can apply masks in two ways: using the Layers panel in Flash Professional or using a display object's mask property in ActionScript. If you use the Layers panel, you can use a single mask layer to affect multiple linked layers below it. If you use ActionScript, each display object requires its own mask.
Creating masks with the Layers panel is easy. Create two or more layers containing overlapping graphics and then activate the top layer as a mask using its Layer Properties. Layer Properties are available by right-clicking the layer or by choosing Modify > Timeline > Layer Properties (see Figure 1).
When a mask layer is activated, the icon next to the layer name changes to indicate that the mask is active and the layer below it indents to show that it's linked to the mask layer. The graphics on the mask layer become invisible and the graphics on the linked layer are visible only through the area defined by the mask layer's graphics.
The mask effect can be seen at author-time as long as both layers remain locked. Unlocking either layer turns off the effect, allowing you to edit the graphics. Additional layers can be linked to the mask layer while working in the Layers panel (see Figure 2).
For more information on using Layers panel to create a mask, see the Using mask layers section of the Flash Professional online help as well as the masks section of the Flash glossary.
You can animate graphics on mask layers as well as masks in ActionScript. In this case, the mask can move over the graphic being masked to reveal parts of that graphic. This is a common approach for creating graphic effects such as spotlights, transitions, and text effects.
The following sample shows how to use a Classic text field to create a text effect over the top of a rectangle containing a gradient fill. Text fields in Flash Professional cannot have gradient fills directly applied to them, so using this technique allows you to create the illusion of that effect. In addition, you can animate the mask graphic and the rectangle graphic to create the effect of text zooming into place while the gradient colors animate within the text shape.
To create a mask effect using a Classic text field:
Note: Be sure to select the Classic Text engine option. TLF Text cannot be used as a mask.
You can animate the text field and the rectangle shape in any way that you like. Experiment with refining the animation using the Motion Editor and Properties panel.
Masks are easy to create in ActionScript. You can use a predefined image or the Drawing API to create a mask graphic that can be assigned to the mask property of any display object.
To create and apply a mask dynamically using ActionScript:
import flash.display.Sprite;
import flash.display.Loader;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
// Create the mask graphic
var maskCircle:Sprite = new Sprite();
maskCircle.graphics.beginFill(0x000000);
maskCircle.graphics.drawEllipse(-50, -50, 100, 100);
maskCircle.graphics.endFill();
maskCircle.visible = false;
addChild(maskCircle);
// Load image
var pictLdr:Loader = new Loader();
var pictURL:String = "http://www.helpexamples.com/flash/images/image1.jpg";
var pictURLReq:URLRequest = new URLRequest(pictURL);
pictLdr.load(pictURLReq);
pictLdr.mask = maskCircle; // Applies the mask
pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
addChild(pictLdr);
// Size the image to fill the Stage
function imgLoaded(event:Event):void
{
pictLdr.width = stage.stageWidth;
pictLdr.height = stage.stageHeight;
}
// Setup the mask to follow the cursor
function mouseMove(event:MouseEvent):void
{
maskCircle.x = stage.mouseX;
maskCircle.y = stage.mouseY;
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
You can use any shape you like for the mask, including an animated movie clip. Apply the mask to any named movie clip instance, whether it loads external content or the content exists in the Library.
This section of the Graphic Effects Learning Guide provides an overview of using masks to create special effects. Spend some time experimenting with animated masks to create effects like spotlights, panning, and cropping.
See the following online resources for more information:
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.