Posted in Adobe, Flash
07/6 2006

AS2 :: BackgroundFill Class

Goal: To devise an easy reusable way to create seamless backgrounds for full-screen Flash movies whether it be gradients or bitmaps. Works with a “bg” Object customized to your needs.

import flash.display.BitmapData;
import flash.geom.Matrix;
 
class org.thesourcecode.utils.BackgroundFill
{
	private var _main:MovieClip;
	private var _sub:MovieClip;
	private var _w:Number;
	private var _h:Number;
	private var _gradient:Object;
	private var _bitmap:String;
	private var _fillType:String;
	private var _colors:Array;
	private var _alphas:Array;
	private var _ratios:Array;
	private var _matrix:Matrix;
	private var _tile:BitmapData;
 
	public function BackgroundFill( clip:MovieClip )
	{
		_main = clip;
	}//end constructor
 
	public function fillBg( bg:Object ):Void
	{
		_w = Stage.width;
		_h = Stage.height;
		_bitmap = bg.bitmap;
		_gradient = bg.gradient;
		_main.clear();
		_main.lineStyle( 1, 0xFFFFFF, 0 );
		_sub.clear();
		_sub.lineStyle( 1, 0xFFFFFF, 0 );
		if (!_gradient)
		{
			_tile = BitmapData.loadBitmap( _bitmap );
			_main.beginBitmapFill( _tile );
			_main.moveTo( 0, 0 );
		} 
		else if (!_bitmap)
		{
			if (_gradient.radial)
			{
				_fillType = "radial";
				_matrix = new Matrix();
				_matrix.createGradientBox( _w, _h, 0, 0, 0 );
			} 
			else 
			{
				_fillType = "linear";
				_matrix = new Matrix();
				_matrix.createGradientBox( _w, _h, 90/180 * Math.PI, 0, 0 );
			}
			//use the defaults for spreadMethod("pad"), interpolationMethod("RGB") and focalPointRatio(0)
			_colors = [ _gradient.c1, _gradient.c2 ];
			_alphas = [ _gradient.a1, _gradient.a2 ];
			_ratios = [ _gradient.r1, _gradient.r2 ];
			_main.beginGradientFill( _fillType, _colors, _alphas, _ratios, _matrix );
		} 
		else 
		{
			_tile = BitmapData.loadBitmap( _bitmap );
			_main.beginBitmapFill( _tile );
			_main.moveTo( 0, 0 );
			if (_gradient.radial)
			{
				_fillType = "radial";
				_matrix = new Matrix();
				_matrix.createGradientBox( _w, _h, 0, 0, 0 );
			} 
			else 
			{
				_fillType = "linear";
				_matrix = new Matrix();
				_matrix.createGradientBox( _w, _h, 90/180 * Math.PI, 0, 0 );
			}
			_colors = [ _gradient.c1, _gradient.c2 ];
			_alphas = [ _gradient.a1, _gradient.a2 ];
			_ratios = [ _gradient.r1, _gradient.r2 ];
			_sub = _main.createEmptyMovieClip( "sub",_main.getNextHighestDepth() );
			_sub.beginGradientFill( _fillType, _colors, _alphas, _ratios, _matrix );
			_sub.lineTo( _w, 0 );
			_sub.lineTo( _w, _h );
			_sub.lineTo( 0, _h );
			_sub.lineTo( 0, 0 );
			_sub.endFill();
		}
		_main.lineTo( _w, 0 );
		_main.lineTo( _w, _h );
		_main.lineTo( 0, _h );
		_main.lineTo( 0, 0 );
		_main.endFill();
	}
}//end class

Download the easy-install extension and sample files here.
Documentation can be found here.

This Post tags: ,

 

USER COMMENTS

Track comments via RSS 2.0 feed. Feel free to post the comment, or trackback from your web site.

Currently there are no comments related to article "AS2 :: BackgroundFill Class".

Get Adobe Flash playerPlugin by wpburn.com wordpress themes