A few important things about Actionscript 3

I haven’t posted in a while, but in the past 6-9 months I’ve been working on some pretty cool stuff. Stratalogica Check it out. Through it I’ve been learning some really nice perks, especially FP10. That along with Python, which is a pretty sweet language… but this is about AS3… maybe some JAVA.

First off Event Priority, that little number we get use to always setting 0 (if you actually set it, otherwise it’s always 0….

this.addEventListener(Event.COMPLETE, someFunction,false,0,true);
//or just
this.addEventListener(Event.COMPLETE,somefunction) //always  0

Anyways sometimes you want certain components to respond before others do, so bump this number up some, the higher the number the higher the priority.

TWO: Printing is not impossible from Flash. Just don’t use the PrintJob class. Use AlivePDF export it out to PDF (you get really nice quality, and font embedding, even advanced font embedding with some Alchemy). If you want a preview of it online, reroute the output and convert the pdf to an image format of your choice on the server side (PHP,JAVA…), and have Flash request it. And it works even easier in AIR, you can even store the made PDF’s into some local SQL DB’s and have even better access to it.

Three: PixelBender rocks. Use it, love it, enjoy it, learn how to use it.

Four: Duplicating Movie clips… and bitmaps, and sprites, and whatever you are “seeing”: I’ve compiled this from multiple sources, and my own tweaking. Crop Whitespace Resizing a Bitmap and more Senocular has been and endless source of knowledge.
Here’s a whole class for all of em:

package
{
	import flash.geom.Rectangle;
	import flash.geom.Matrix;
	import flash.display.IBitmapDrawable;
	import flash.display.DisplayObject;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.display.Sprite;
 
	/**
	 * @author jbonfante
	 */
	public class DuplicateDisplayObject
	{
		public static function duplicateImageAsSprite( original : DisplayObject ) : Sprite
		{
 
			var bitmapData : BitmapData = new BitmapData(original.width, original.height, true, 0x000000);
			bitmapData.draw(original as IBitmapDrawable);
			var bitmap : Bitmap = getBitmapCroppedOutWhitespace(bitmapData);
			var returnSprite : Sprite = new Sprite();
			returnSprite.addChild(bitmap as DisplayObject);
			returnSprite.x = original.x;
			returnSprite.y = original.y;
			return returnSprite;
		}
 
		public static function duplicateImage( original : DisplayObject ) : Sprite
		{
			var bitmapData : BitmapData = new BitmapData(original.width, original.height, true, 0x000000);
			bitmapData.draw(original as IBitmapDrawable);
 
			var bitmap : Bitmap = getBitmapCroppedOutWhitespace(bitmapData);
 
			var returnSprite : Sprite = new Sprite();
			returnSprite.addChild(bitmap as DisplayObject);
			returnSprite.cacheAsBitmap = true;
			return returnSprite;
		}
 
		public static function ScaledDuplicate( original : DisplayObject, bitmapScale : Number ) : Sprite
		{
			var bitmapData : BitmapData = new BitmapData(original.width, original.height, true, 0x000000);
			bitmapData.draw(original as IBitmapDrawable);
			var scaleFactor : Number = bitmapScale;
			var newWidth : Number a bitmapData.width * scaleFactor;
			var newHeight : Number = bitmapData.height * scaleFactor;
			var scaledBitmapData : BitmapData = new BitmapData(newWidth, newHeight, true, 0xFFFFFFFF);
			var scaleMatrix : Matrix = new Matrix();
			scaleMatrix.scale(scaleFactor, scaleFactor);
			scaledBitmapData.draw(bitmapData, scaleMatrix);
 
			var bitmap : Bitmap = new Bitmap(scaledBitmapData);
 
			var returnSprite : Sprite = new Sprite();
			returnSprite.addChild(bitmap as DisplayObject);
			returnSprite.cacheAsBitmap = true;
			return returnSprite;
		}
 
		public static function ScaledBitmap(bitmapScale : Number, _originalBitmap : Bitmap) : BitmapData
		{
			var originalBitmapData : BitmapData = _originalBitmap.bitmapData;
			var scaleFactor : Number = bitmapScale;
			var newWidth : Number = originalBitmapData.width * scaleFactor;
			var newHeight : Number = originalBitmapData.height * scaleFactor;
			var scaledBitmapData : BitmapData = new BitmapData(newWidth, newHeight, true, 0xFFFFFFFF);
			var scaleMatrix : Matrix = new Matrix();
			scaleMatrix.scale(scaleFactor, scaleFactor);
			scaledBitmapData.draw(originalBitmapData, scaleMatrix);
			return scaledBitmapData;
		}
 
		public static function getBitmapCroppedOutWhitespace(bd : BitmapData) : Bitmap
		{
			var bmd : BitmapData = new BitmapData(bd.width, bd.height, true, 0x00000000);
			bmd.draw(bd);
 
			var rect : Rectangle = bmd.getColorBoundsRect(0xFFFFFF, 0x00000000, false);
			var bmd2 : BitmapData = new BitmapData(rect.width+200, rect.height+200, true, 0x00000000);
			bmd2 cialis generique belgique.draw(bmd, new Matrix(1, 0, 0, 1, -rect.x, -rect.y));
 
			var bmp : Bitmap = new Bitmap(bmd2);
 			bmp.x = 100;
 			bmp.y = 100;
			return bmp;
		}
	}
}

Five: Vertical Gradient Fills! Way easier in AS3, than trying to do it in the IDE… here you go, thanks to Darren’s Blog Piston hog posted a nice little comment:

var shape1:Shape = new Shape();
var matrix:Matrix = new Matrix();
matrix.createGradientBox(163, 24, (Math.PI/180)*90, 0, 00);
var colors:Array = [ 0xffffff, 0x000000];
var alphas:Array = [ 1.0, 1.0];
var ratios:Array = [ 0, 255];
shape1.graphics.lineStyle(2,0xa1b0b6);
shape1.graphics.beginGradientFill(GradientType.LINEAR,colors, alphas, ratios, matrix);
shape1.graphics.drawRect( 0.0, 0.0, 163, 24);
shape1.graphics.endFill();
addChild(shape1);

Enjoy, more’s is coming.