AS2 → AS3: Setting Masks Dynamically

Download Example Files

The more migrating I do to AS3 the more little nuances (at least nuances when I am migrating) I find. A lot of small little methods have been switched or moved into new classes and it's always a bit of a drag to find them. Such is the case with the setMask() command.

Although setMask() was a very simple command in AS2, it is even easier in AS3. The following is really a basic overview and there really isn't much more to it.

Let's look at the AS2 code for setting a mask dynamically with setMask():

  1. box_mc.setMask(circle_mc);

As simple as that is, it is even simpler in AS3:

  1. box_mc.mask = circle_mc;

This not only looks cleaner but makes a lot more sense. After fumbling around with trying to find where setMask() went, this was a great solution.

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.


cool one!

Great! Thanks!


good find. thanks for that.

what about i have a couple mc needs tobe masked and i have one masker, how it is work that way

@viaria: Unfortunately it doesnt seem you can do this. your two options are putting all your mcs into one mc and masking that mc or duplicating the masks and setting each one individually. kinda of sucks, but thats the only way around it.

i have already done that, and i made reflect effect them but when the animation finishes, and i click to stage, reflections alpha going down except the last element in the movieclip stay as it is, i dont event have click event, this is not my mistake it's about adobe,i tried everything almost, dynamic r frame animation, it is sad

Thanks for this tip, quick and easy

I also would add if you want to move or add alphas to your mask you can include this code:


mask_mc.mask = maskee_mc;

@ira, is there any way shorter?, such as doing it in one line without a masker..

masked_mc.alpha = .5; ->
works i guess...

excelente jeje gracias

Even easier, apply a gradient fill directly behind 1 or more dynamic text boxes for the text to have the fill. No need for the text to be in a movieclip.
mask_mc.mask = txtBox;

thanks a lot. helped me.

Your phrase "it is not even easier in AS3" means the opposite of what you meant. Should have said "is even easier in AS3"

@Arby: Good catch! Fixed 🙂

Code works, but for some reason my mask does not work propperly, it flashes: it displays on and off randomly, as if the mask had gaps, but that is in fact not the case. Absolutely no idea why this happens. Any thoughts?

Leave a comment