AS3 Code Snippets

The code snippet repository is my contribution to the community. These are functions that I tend to re-use and could easily be made into static functions and placed into a utility class, but I’ll leave that up to you. Check back often, for I while keep updating this posting when I have time between gigs.

Remove Children from a display list
/*
Method: removeAllChildren
Parameters:
inDsp:DisplayObject
Returns:
*/

private function removeAllChildren(inDsp:*):void
{
   var child:int = inDsp.numChildren;
   while( child -- )
   {
  inDsp.removeChildAt(child);
   }
}

Flips a display object horizontally.
/*
Method:flipHorizontal
Parameters:
dsp:DisplayObject
Return:
*/

private function flipHorizontal(dsp:*):void
{
   var matrix:Matrix = dsp.transform.matrix;
   matrix.a=-1;
   matrix.tx=dsp.width+dsp.x;
   dsp.transform.matrix=matrix;
}

Draws a rectangle within a display object.
/*
Method:createRectangle
Parameters:
src:*
inColor:uint
inW:Number;
inH:Number;
Return:
*/

public function createRectangle(src:* ,inColor:uint =0x999999,inW:Number=50,inH:Number=50):void
{
    var rect:Shape = new Shape();
    rect.graphics.clear();
    rect.graphics.beginFill(inColor);
    rect.graphics.drawRect(0,0,inW,inH);
    rect.graphics.endFill();
    src.addChild(rect);
}

Draws a Circle within a display object.
/*
Method:createCircle
Parameters:
inSrc:*
inColor:uint
inRadius:Number
Return:
*/

public function createCircle(inSrc:*,inColor:uint=0x999999,inRadius:Number=25):void
{
      var circ:Shape = new Shape();
      circ.graphics.clear();
      circ.graphics.beginFill(inColor);
      circ.graphics.drawEllipse(0,0, inRadius, inRadius);
      circ.graphics.endFill();
      inSrc.addChild(circ);
}

Draws a Stroke within a display object.
/*
Method:createStroke
Parameters:
src:*
inStrokeWidth:Number
inColor:uint
inW:Number;
inH:Number;
Return:
*/

public function createStroke(src:* ,inStrokeWidth:Number=2,inColor:uint =0x9999FF,inW:Number=50,inH:Number=50):void
{
    var stroke = new Shape();
    stroke.graphics.clear();
    stroke.graphics.lineStyle(inStrokeWidth, inColor, 1);
    stroke.graphics.moveTo(0, 0);
    stroke.graphics.lineTo(inW, 0);
    stroke.graphics.lineTo(inW, inH);
    stroke.graphics.lineTo(0, inH);
    stroke.graphics.lineTo(0, 0);
    src.addChild(stroke);
}

Take a snap shot of a Dispaly Object and returns a bitmap.
/*
Method:snapClip
Parameters:
src:*
Return:
Bitmap
*/

public function snapClip( src:* ):Bitmap
{
    var bmp:Bitmap;
    var bounds:Rectangle = src.getBounds( src );
    var bmpData:BitmapData = new BitmapData( int( bounds.width + 0.5 ), int( bounds.height + 0.5 ), true, 0 );
    bmpData.draw( src, new Matrix(1,0,0,1,-bounds.x,-bounds.y) );
    bmp = new Bitmap(bmpData,"auto",true);
    return bmp;
}


Group and cache items in a display object.

/*
Method: groupAndCache
Parameters:
src:*
Return:
*/

   public function groupAndCache(src:*):void{
     
    src.cacheAsBitmap = true;
    var bmData:BitmapData = new BitmapData(src.width, src.height, true, 0x000000);
    bmData.draw(src);    
    var bm:Bitmap = new Bitmap(bmData, "auto", true);    
    while(src.numChildren > 0){
      src.removeChildAt(0);
    }  
    src.addChild(bm);
}


Mask a display object.

/*
Method:maskDisplayObject
Parameters:
inSrc:*
inW:Number;
inH:Number;
Return:
*/

private function maskDisplayObject(inSrc:*,inXposition:Number,inYposition:Number,inW:Number,inH:Number):void {
var theMask=new Sprite () ;
     theMask.graphics.clear();
     theMask.graphics.beginFill(0xFFFFFF);
     theMask.graphics.drawRect(inXposition,inYposition,inW,inH);
     theMask.graphics.endFill();
     inSrc.mask=theMask;
}


Adds a drop shadow to a display object.

/*
Method:addDropShadowToDisplayObject
Parameters:
inSrc:*
inDistance:Number
inAngle:Number
inColor:uint
inAlpha:Number
inBlurX:Number
inBlurY:Number
Return:
*/

private function addDropShadowToDisplayObject(inSrc:*,inDistance:Number=1,inAngle:Number=45,inColor:uint=0x000000,inAlpha:Number=1,inBlurX:Number=5.0,inBlurY:Number=5.0):void
{
  var dropShadow:DropShadowFilter = new DropShadowFilter();
  dropShadow.distance = inDistance;
  dropShadow.angle = inAngle
  dropShadow.alpha = inAlpha;
  dropShadow.blurX = inBlurX;
  dropShadow.blurY = inBlurY;
  dropShadow.color = inColor;
        inSrc.filters = [dropShadow];
}


Blurs a display object.

/*
Method:blurDisplayObject
Parameters:
inSrc:*
inBlurX:Number
inBlurY:Number
Return:
*/

private function blurDisplayObject(inSrc:*,inBlurX:Number = 5,inBlurY:Number=5):void
{
  var filter:BlurFilter=new BlurFilter(inBlurX,inBlurY,BitmapFilterQuality.HIGH);
        inSrc.filters = [filter];
}


Applies a hyperlink to a standard textfield.

/*
Method: createHyperLink
Parameters:
inTField:TextField
inUrl:String
inColor:uint
Returns:
*/

private function createHyperLink(inTField:TextField,inUrl:String,inColor:uint=0x0000FF):void
{
  var tFormat:TextFormat = new TextFormat();
  tFormat.color = inColor;
  tFormat.underline = true;
  tFormat.url = inUrl;
  inTField.setTextFormat(tFormat);
}


Creates a radial gradient in a display object.

/*
Method:createRadialGradient
Parameters:
inSrc:*
inColorArray:Array
inRadius:Number
Return:
*/

private function createRadialGradient(inSrc:*,inColorArray:Array,inRadius:Number = 50):void
{
  var colorArray:Array =inColorArray;
  var alphas:Array =[1,1];
  var ratios:Array =[0,255];
  var matr = new Matrix();
  matr.createGradientBox(2*inRadius,2*inRadius,0,-inRadius,-inRadius);
  var circ:Shape = new Shape();
  circ.graphics.clear();
  circ.graphics.beginGradientFill(GradientType.RADIAL,colorArray,alphas,ratios,matr);
  circ.graphics.drawCircle(0,0,inRadius);
  circ.graphics.endFill();
  inSrc.addChild(circ );
}


Creates a linear gradient in a display object.

/*
Method:createLinearGradientRectangle
Parameters:
inSrc:*
inColorArray:Array
inRotation:Number
inW:Number
inH:Number
Return:
*/

private function createLinearGradient(inSrc:*,inColorArray:Array,inRotation:Number=-90,inW:Number=50,inH:Number=50):void
{
   var colorArray = inColorArray;
   var alphas:Array = [ 1, 1 ];
   var ratios:Array = [ 0, 255 ];
   var rotate:Number = inRotation*Math.PI/180;
   var matr:Matrix = new Matrix();
   matr.createGradientBox( inW, inH, rotate, 0, 0 );
   var sprMethod:String = SpreadMethod.PAD;
   var spr:Shape = new Shape();
   spr.graphics.clear();
   spr.graphics.beginGradientFill( GradientType.LINEAR, colorArray, alphas, ratios, matr, sprMethod );
   spr.graphics.drawRect( 0, 0, inW, inH );
   spr.graphics.endFill();

  inSrc.addChild( spr );     
}


Change the color of a display object.

/*
Method:changeDisplayObjectColor
Parameters:
inSrc:*
inColor:uint
Return:
*/

private function changeDisplayObjectColor(inSrc:*,inColor:uint= 0x000000):void
{  
  var colorTrans:ColorTransform = inSrc.transform.colorTransform;
  colorTrans.color = inColor;
        inSrc.transform.colorTransform = colorTrans;
}


Formats time into 000:00.

/*
method:formatTime
Parameters:
time:Number
returns: String
*/

private function formatTime (time:Number):String
{
  var remainder:Number;
  var hours:Number = time / ( 60 * 60 );
  remainder = hours - (Math.floor ( hours ));
  hours = Math.floor ( hours );
  var minutes = remainder * 60;
  remainder = minutes - (Math.floor ( minutes ));
  minutes = Math.floor ( minutes );
  var seconds = remainder * 60;
  remainder = seconds - (Math.floor ( seconds ));
  seconds = Math.floor ( seconds );
  var hString:String = hours < 10 ? "0" + hours : "" + hours;
  var mString:String = minutes < 10 ? "0" + minutes : "" + minutes;
  var sString:String = seconds < 10 ? "0" + seconds : "" + seconds;
  if ( time < 0 || isNaN(time)) return "00:00";
  if ( hours > 0 )
  {    
  return hString + ":" + mString + ":" + sString;
  }else{
  return mString + ":" + sString;
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: