Dynamic mask objects have been key to many Flash project’s success. When it comes to migrating over from AS2 to AS3, you’ll find that things have changed as the method “setMask()” in AS2 is now a property called “mask”. In this next edition of the Moving From AS2 to AS3 series, I will be comparing the two solutions and offering an efficient solution to creating dynamic masks using AS3.
Comparing AS2 to AS3
Setting masks dynamically using ActionScript 2.0
Setting masks dynamically using ActionScript 3.0
myMC.mask = maskObject;
So as you can see the syntax hasn’t changed much, however, personally I feel that AS3’s implementation is a bit more literal making it easier to understand.
Creating your masking objects as Sprites
While in ActionScript 2.0 there was no better of a solution than to create your masking object as a MovieClip. With the introduction of the Sprite object in ActionScript 3.0, creating your masking objects as Sprites is a far more efficient solution.
Since a mask object does not require a timeline, using a Sprite instead of a MovieClip simply makes more sense. Below is an example where a Sprite is used in place of a MovieClip:
// Create mask var maskObject:Sprite = new Sprite(); maskObject.graphics.beginFill(0xFF0000); maskObject.graphics.drawRoundRect(myMC.x, myMC.y, 453, 290, 20); // Apply mask myMC.mask = maskObject;
I hope this helps in your migration over into AS3. If there is something in particular you want to add or see with the Moving From AS2 to AS3 series, feel free to contact me via the contact page.