View comments | RSS feed

MovieClip.setMask()

Availability

Flash Player 6.

Usage

my_mc.setMask(mask_mc:Object) : Void

Parameters

my_mc The instance name of a movie clip to be masked.

mask_mc The instance name of a movie clip to be a mask.

Returns

Nothing.

Description

Method; makes the movie clip in the parameter mask_mc a mask that reveals the movie clip specified by the my_mc parameter.

This method allows multiple-frame movie clips with complex, multilayered content to act as masks. You can shut masks on and off at runtime. However, you can't use the same mask for multiple masks (which is possible by using mask layers). If you have device fonts in a masked movie clip, they are drawn but not masked. You can't set a movie clip to be its own mask--for example, my_mc.setMask(my_mc).

If you create a mask layer that contains a movie clip, and then apply the setMask() method to it, the setMask() call takes priority and this is not reversible. For example, you could have a movie clip in a mask layer called UIMask that masks another layer containing another movie clip called UIMaskee. If, as the SWF file plays, you call UIMask.setMask(UIMaskee), from that point on, UIMask is masked by UIMaskee.

To cancel a mask created with ActionScript, pass the value null to the setMask() method. The following code cancels the mask without affecting the mask layer in the Timeline.

UIMask.setMask(null);

You can extend the methods and event handlers of the MovieClip class by creating a subclass. For more information, see "Assigning a class to a movie clip symbol" in Using ActionScript in Flash.

Example

The following code uses the movie clip circleMask_mc to mask the movie clip theMaskee_mc:

theMaskee_mc.setMask(circleMask_mc);

Comments


recoveredfromflashMX2004 said on Aug 4, 2004 at 3:31 PM :
No screen name said on Mar 21, 2004 at 3:17 AM :

There seems to be a bug in the way masking is handled:

Let's say I have a movie clip called "result". In it I have two movie clips called "mc" and "mask" and I mask "mc" using "mask" (either via layers mask or with setMask()).

Intuitevely, the _width, _height, _x, _y and getBounds() of the result movie clip should match only the visible part, but it's not the case. If "mc" is 200px wide and "mask" is 10px wide, on stage the Property panel says that "result" has a width of 10px, but in actionscript result._width returns 200px.

To make things even more complicated, mouse events seem to work correctly, i.e. only the visible part of "result" reacts to an onPress event and a hitTest between "result" and a coordinate outside the visible area works fine -- but a hitTest between two movie clips is also made on the full size of the "result" not just the visible area.


jepo said on Apr 1, 2004 at 1:55 PM :

This should be working as expected. The _width, _height, _x and _y and getBounds of the result movie clip should match the dimensions of the movie clip instead of the mask. But if you hitTest the mask instead of the movie clip itself, it will be hitTest. If you use the masks width and height, then it works as expected. Thanks!


flash-hero said on May 21, 2004 at 10:37 PM :

The setMask command works in a wrong way when you use dynamic load of jpg.
example 1. If you use loadMovie on MovieClip pic1_mc and load a jpg and use the setMask on pic1_mc it is not working because the picture takes over the mc. The solution is of course to create a new mc pic2_mc inside pic1_mc and then use pic2_mc.loadMovie and pic1_mc.setMask.

Example 2. If you build a jpg-viewer using masks this way (ex 1) and you want to load this movie into another website/movie, then the same problem arises - the mask disappears. This time you can't fix the problem inside the viewer. The solution is now this (strange one):
First create the viewer application without masks inside. Then load the viewer into the flash-website. In the _root/flashwebsite construct the mask and put it to the viewer area where it must be masked. Again be careful because now the same thing happens as in example 1. If you are not using a mc inside a mc for loadMovie, then setMask will not work because of the loading.
For me it looks as the loadMovie changes the mc to something different. I think it should be possible to check what it is, by using the for-in loop to see the properties and methods after loading, but I did not try this.


fh
romanticos said on Sep 11, 2004 at 3:18 AM :
It would be nice if there was a complementary getMask() function for MovieClip.setMask().
Fred the boolean said on Sep 28, 2004 at 7:27 PM :
you might want to try using the following code for a getMask method
The code will only work if you setMask using actionScript it won't get a mask you set on the stage

// code by robin Fleming robin@crankmedia.com.au
if (!_global.movieMaskClips)
{
_global.movieMaskClips = new MovieClip();
MovieClip.prototype.s_setMask = _global.movieMaskClips.setMask;
}

MovieClip.prototype.setMask = function (m_mask)
{
this.maskingClip = m_mask;
this.s_setMask(m_mask)
};

MovieClip.prototype.getMask = function ()
{
return this.maskingClip;
};
shimi2 said on Nov 17, 2004 at 11:50 AM :
Tip: Setting MovieClip._visible to false does not cancel a mask. To cancel a mask, use setMask(null).
Note, however, that setMask(null) cancels the mask effect, but the mask MC remains visible in the SWF.
To cancel a mask and hide it in the SWF:
mask_mc.setMask(null);
mask_mc._visible = false;
dgmclaren said on Dec 15, 2004 at 9:18 AM :
I was able to do a "union" mask where the maskee is revealed by two movie clips contained in a third (call it mc3).
mc3 contains mask1 and mask2
maskee.setMask(mc3);

What I need to do now is an "intersect" mask. Unfortunately, the following doesn't work:
mask1.setMask(mask2);
maskee.setMask(mask1);

Does anyone have any brilliant ideas? Thanks in advance!

-Daniel
jason4short said on Jan 6, 2005 at 11:34 AM :
Masked MovieClips do not report their correct _width or _height.
Even if this were the "correct" behavior I would appreciate being able to
know the correct width of the visible portion of a MovieClip. Sometimes
the workarounds can get ugly.
Vandy said on Jan 11, 2005 at 2:02 PM :
Mask problem in MX2004
The following code worked in FlashMX but no longer in MX2004:

createEmptyMovieClip("graphBars", 100);
createEmptyMovieClip("maskIt",101);
with (maskIt) {
beginFill(20,0);
moveTo(20,0);
lineTo(650,0);
lineTo(650,380);
lineTo(20,380);
endFill();
}
graphBars.setMask(maskIt);

No matter what I tried, I could not get Flash to mask movie clips attached to graphBars. Any bright ideas? Thx. D
Francis Cheng said on Jan 16, 2005 at 8:57 PM :
Hi jason4short,
The ability to do that would certainly be handy, I'd suggest entering a feature request here:
http://www.macromedia.com/support/email/wishform/
You could also try posting in the online support forums:
http://webforums.macromedia.com/flash
pam051071 said on Feb 10, 2005 at 12:53 PM :
Using the setMask method with dynamic movieclips seems to work for me.

Try this:

this.createEmptyMovieClip("square_mc", 1);
with (square_mc) {
beginFill(0xFF0000);
moveTo(0, 0);
lineTo(300, 0);
lineTo(300, 370);
lineTo(0, 370);
lineTo(0, 0);
endFill();
}
this.createEmptyMovieClip("text_mc", 0);
text_mc.createTextField("my_txt", 0, 0, 0, 300, 600);
text_mc.setMask(square_mc);
with (text_mc.my_txt) {
multiline = false;
border = false;
wordWrap = true;
}
text_mc.my_txt.text = "Nam liber te conscient to factor tum poen legum odioque civiuda. Et tam neque pecun modut est neque nonor et imper ned libidig met,consectetur adipiscing elit, sed ut labore et dolore magna aliquam makes one wonder who would ever read this stuff?Nam liber te conscient to factor tum poen legum odioque civiuda. Et tam neque pecun modut est neque nonor et imper ned libidig met,consectetur adipiscing elit, sed ut labore et dolore magna aliquam makes one wonder who would ever read this stuff?Nam liber te conscient to factor tum poen legum odioque civiuda. Et tam neque pecun modut est neque nonor et imper ned libidig met,consectetur adipiscing elit, sed ut labore et dolore magna aliquam makes one wonder who would ever read this stuff?Nam liber te conscient to factor tum poen legum odioque civiuda. Et tam neque pecun modut est neque nonor et imper ned libidig met,consectetur adipiscing elit, sed ut labore et dolore magna aliquam makes one wonder who would ever read this stuff?Nam liber te conscient to factor tum poen legum odioque civiuda. Et tam neque pecun modut est neque nonor et imper ned libidig met,consectetur adipiscing elit, sed ut labore et dolore magna aliquam makes one wonder who would ever read this stuff?Nam liber te conscient to factor tum poen legum odioque civiuda. Et tam neque pecun modut est neque nonor et imper ned libidig met,consectetur adipiscing elit, sed ut labore et dolore magna aliquam makes one wonder who would ever read this stuff?Nam liber te conscient to factor tum poen legum odioque civiuda. Et tam neque pecun modut est neque nonor et imper ned libidig met,consectetur adipiscing elit, sed ut labore et dolore magna aliquam makes one wonder who would ever read this stuff?Nam liber te conscient to factor tum poen legum odioque civiuda. Et tam neque pecun modut est neque nonor et imper ned libidig met,consectetur adipiscing elit, sed ut labore et dolore magna aliquam makes one wonder who would ever read this stuff?Nam liber te conscient to factor tum poen legum odioque civiuda. Et tam neque pecun modut est neque nonor et imper ned libidig met,consectetur adipiscing elit, sed ut labore et dolore magna aliquam makes one wonder who would ever read this stuff?Nam liber te conscient to factor tum poen legum odioque civiuda. Et tam neque pecun modut est neque nonor et imper ned libidig met,consectetur adipiscing elit, sed ut labore et dolore magna aliquam makes one wonder who would ever read this stuff?Nam liber te conscient to factor tum poen legum odioque civiuda. Et tam neque pecun modut est neque nonor et
rmw_ask said on Apr 7, 2005 at 10:49 PM :
I'm having a different issue using a movieclip containing exclusively runtime fills as a mask for an image. I'm attempting to draw a mask with rounded corners, but the mask seems to be using the entire bounding box of the clip
divineqixotic said on May 12, 2005 at 8:35 AM :
Hope this helps someone in trouble.
While masking movieClips loaded in levells use something like this or else it wont work.
[For movie loaded in level 100 , both clips in smae level]
toMask.setMask("_level100.mask_mc");

Please can we document it. Wasted more than an hour to sort this out. :-(
Ergates99 said on May 24, 2005 at 8:49 AM :
There are terminally serious issues with this in a true OOP environment. If you create your movieclips using createEmptyMovieClip and load the mask and maskee content dynamically it simply doesn't work at all. The closest I can get is to load the maskee content dynamically whilst retaining the mask clips on the timeline. Having created an AS 2.0 panorama class for use in games which was to have been entirely portable and multi layered, I'm ever so slightly irritated by this to say the least ;-(

Any workaround ideas anyone?
square-01 said on Jun 5, 2005 at 12:28 PM :
I'm not sure if Ergates99 and I are having the same issue or not, but I
can't get the setMask to work when I'm using attachMovie for both the
mask and maskee.

I've tried placing it upon the first frame of me _root timeline prior to
the function that houses the attachMovie command, as well as placing
it INSIDE the function that calls the attachMovie. I've also tried
preplacing it on the stage and setting _alpha= false until I need it, then
toggle the _alpha to 100.


Any thoughts?
slifty86 said on Jul 18, 2005 at 8:11 AM :
I'm not entirely sure of the technicalities, but this worked for me as a workaround.

_root.createEmptyMovieClip("image_mc",1);
_root.createEmptyMovieClip("mask_mc",2);

image_mc.createEmptyMovieClip("imageHolder_mc",1);

image_mc.setMask(mask_mc);

with(_root.mask_mc) {
beginFill(0xFF0000,100);
moveTo(0,0);
lineTo (100, 0);
lineTo (100, 100);
lineTo (0, 100);
lineTo (0,0);
endFill();
}

Then you can load the image into imageHolder_mc rather than image_mc. (accessing imageHolder_mc via image_mc.imageHolder_mc)

I guess when you load an image into a movie clip it changes the way it can interact with masks, so if you are adding content to the clip dynamically, be sure to run any loading functions on a clip INSIDE the clip that you want masked

Hope this helps in some way!
square-01 said on Jul 18, 2005 at 10:53 PM :
Thanks Slifty86. I have since found a workaround for my problem. Now,
I'll be honest. I'm a total hack at coding, managing to piece together code
here and there (duct tape included!) to get the endproduct I'm after. I'm
sure there are better ways, but this is what I came up with. Please let me
know if it's just an absolutely ridiculous waste of code. Basically I create
a function, then wiithin that function, make an onEnterFrame command
that I then try to "kill" once it's been successful. Is this the correct way of
going about this? I ran into serious trouble on on project that was
SUPERHEAVY on the animations with LOTS of onEnterFrames.
Someone told me to "kill" them once they are used.

Code:

[as]
function loadMusicPortfolio(){
var maskStatus;
_root.attachMovie("mc_1", "mc_Mask1", 161, {_x:195,_y:
578,_height:50,_width:677,_alpha:0});//Attatches the mask to the stage
_root.attachMovie("mc_2", "mc_1", 160, {_x:195,_y:578});//Attatches
the movie to be masked to the stage
this.onEnterFrame = function(){ //loads setMask once mask
and maskee are loaded or "initialized"
_root.mc_1.setMask(mc_Mask1);
maskStatus = "1";
if (maskStatus = "1") {
trace("mask is active");
delete this.onEnterFrame;
}
}
}
[/as]
Peter Strømberg said on Sep 27, 2005 at 1:08 PM :
It appears when you draw a dynamic mask Flash uses it's bounding box as the mask area. It is however possible to amke masks with cutouts, as long as you avoid using a moveTo.
The following example masks a movie clip "box" in the top left corner of the screen (you need to create a movie clip and call it "box" for it to work :O)

this.createEmptyMovieClip("mask", 1);
with (mask) {
beginFill(0xFF0000);
moveTo(0, 0);
lineTo(300, 0);
lineTo(300, 300);
lineTo(0, 300);
lineTo(0, 50);
lineTo(50,50);
lineTo(50,10);
lineTo(10,10);
lineTo(10,50);
lineTo(0,50);
lineTo(0,0);
endFill();
}
box.setMask(mask);

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001551.html