Dinamic Mask with ActionScript 3
24/10/2008
Masking allows you to hide part of an object or text
This tutorial will show you the basics of masking in Flash and flex with ActionScript 3 through mi class called Msk
First we import the Msk Class:
import phoxer.ChildsManager.Msk;<br /> //with this simple line we can mask an object var msk:Msk= new Msk(this,MySprite,0,0,150,100,20); //o we can implement like this too var msk:Msk= new Msk(this); msk.setNewMask(MySprite,0,0,150,100,20);
1- The first parameter is used to addChild the mask
2- The second parameter is the Object that is going to be masked
3- The third and fourth parameter is the x and y coordinates of the mask
4- The fourth and fifth parameter is the width and height of the mask
5- the Sext param is optional, is to round the edges.
5- the last param is optional and make mask visible.
Here is the Class for Flash:
/** by .:{PHOXER}:. http://www.phoxer.com v 3.0; */ package phoxer.Sprites{ import flash.display.Shape; import flash.display.Sprite; import phoxer.ChildsManager.DeleteClips; public class Msk extends Sprite{ private var cuad:Shape; public function Msk(cont:Object,mc:Object=null,x:int=0,y:int=0,w:int=0,h:int=0,rad:int=0,vs:Boolean=false):void{ super(); cont.addChild(this); this.mouseEnabled=false; this.mouseChildren=false; if(mc!=null){ setNewMask(mc,x,y,w,h,rad,vs); } } public function setNewMask(mc:Object,x:int,y:int,w:int,h:int,rad:int=0,vs:Boolean=false):void{ DeleteClips.deleteAll(this); this.x=mc.x; this.y=mc.y; cuad = new Shape(); cuad.graphics.beginFill(0x000000); cuad.graphics.drawRoundRect(x, y, w, h,rad); cuad.graphics.endFill(); this.addChild(cuad); if(!vs){ mc.mask=this; } } public function addMsk(x:int,y:int,w:int,h:int,rad:int=0):void{ cuad = new Shape(); cuad.graphics.beginFill(0x000000); cuad.graphics.drawRoundRect(x, y, w, h,rad); cuad.graphics.endFill(); this.addChild(cuad); } } }
Also i developed a class in ActionScript 3 to handle masks for example in Flex extending UIMovieClip:
/** by .:{PHOXER}:. http://www.phoxer.com v 3.0; */ package phoxer.Sprites{ import flash.display.Shape; import mx.flash.UIMovieClip; import phoxer.ChildsManager.DeleteClips; public class FlexMsk extends UIMovieClip{ private var cuad:Shape; public function FlexMsk(cont:Object,mc:Object=null,x:int=0,y:int=0,w:int=0,h:int=0,rad:int=0,vs:Boolean=false):void{ super(); cont.addChild(this); this.mouseEnabled=false; this.mouseChildren=false; if(mc!=null){ setNewMask(mc,x,y,w,h,rad,vs); } } public function setNewMask(mc:Object,x:int,y:int,w:int,h:int,rad:int=0,vs:Boolean=false):void{ DeleteClips.deleteAll(this); this.x=mc.x; this.y=mc.y; cuad = new Shape(); cuad.graphics.beginFill(0x000000); cuad.graphics.drawRoundRect(x, y, w, h,rad); cuad.graphics.endFill(); this.addChild(cuad); if(!vs){ mc.mask=this; } } public function addMsk(x:int,y:int,w:int,h:int,rad:int=0):void{ cuad = new Shape(); cuad.graphics.beginFill(0x000000); cuad.graphics.drawRoundRect(x, y, w, h,rad); cuad.graphics.endFill(); this.addChild(cuad); } } }
