Pie Preloader Animation in Actionscript 3.0

PiePreloader.as 

package
{
	import flash.display.Shape;

	public class PiePreloader extends Shape
	{
		private var _radius:Number = 0;
		private var _bc    :int    = 0;
		private var _fc    :int    = 0;
		private var _bs    :int    = 0;

		public function PiePreloader($radius:Number, $border_size:int, $border_color:int, $fill_color:int)
		{
			_radius = $radius;
			_bs     = $border_size;
			_bc     = $border_color;
			_fc     = $fill_color;
		}

		public function Draw($e:Number):void
		{
			var $end   = 2*Math.PI*$e/100;

			this.graphics.clear();

			if ($end != 0)
			{
				this.graphics.lineStyle(_bs,_bc);
				this.graphics.beginFill(_fc);
				this.graphics.lineTo(_radius,0);

				Curve(0, 0, 0, $end);

				this.graphics.lineTo(0,0);
				this.graphics.endFill();
			}
		}

		private function Curve($x:Number, $y:Number, $start:Number, $end:Number)
		{
			 var diff = Math.abs($end -$start);
			 var divs = Math.floor(diff/(Math.PI/4))+1;
			 var span = -(diff/(2*divs));
			 var rc   = _radius/Math.cos(span);

			 this.graphics.moveTo(Math.cos($start)*_radius, Math.sin($start)*_radius);

			 for (var i=0; i<divs; ++i)
			 {
				  $end = $start+span;
				  $start = $end+span;
				  this.graphics.curveTo(Math.cos($end)*rc, Math.sin($end)*rc, Math.cos($start)*_radius, Math.sin($start)*_radius);
			 };

			 return this;
		};
	}
}

How to use it:

var _preloader:PiePreloader = new PiePreloader(35, 1, 0x000000, 0x74C2E1);
_preloader.x = stage.stageWidth/2;
_preloader.y = stage.stageHeight/2;
addChild(_preloader);


var _loader:Loader = new Loader();
_loader.load(new URLRequest("punk.jpg"));
_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, Progress, false, 0, true);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,         Complete, false, 0, true);


function Progress($e:Event):void
{
	var $p:Number = $e.target.bytesLoaded/$e.target.bytesTotal*100;
	_preloader.Draw($p);
}

function Complete($e:Event):void
{
	addChild(_loader)
}
Advertisements

One thought on “Pie Preloader Animation in Actionscript 3.0

  1. Pingback: Pie Preloader Animation in Actionscript 3.0 « code.zombie | Flash Designers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s