Return to Snippet

Revision: 40785
at February 8, 2011 02:17 by kuril


Initial Code
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.utils.Timer;

public class PreloaderCamomile extends Sprite
{
	private static const RAD:Number = Math.PI / 180;
	private const petalsCount:int = 12;

	private var radius:int;
	private var innerRadius:int;
	private var petalGage:Number;
	private var color:uint;

	private const petals:Vector.<Shape> = new Vector.<Shape>;
	private const timer:Timer = new Timer(80);

	public function PreloaderCamomile(size:int = 20, color:uint = 0)
	{
		radius = size * .5;
		innerRadius = radius * .42;
		petalGage = 360 / petalsCount;
		this.color = color;
		addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
	}

	private function addedToStageHandler(event:Event):void
	{
		for (var i:int = 1; i <= petalsCount; i++)
		{
			var petal:Shape = new Shape();
			drawPetal(petal.graphics, i * petalGage);
			addChild(petal);
			petals.push(petal);
			petal.alpha = 0;
		}
		timer.addEventListener(TimerEvent.TIMER, timerEventHandler);
		timer.start();
	}

	private function removedFromStageHandler(event:Event):void
	{
		timer.removeEventListener(TimerEvent.TIMER, timerEventHandler);
		timer.stop();
		while (petals.length)
		{
			removeChild(petals.shift());
		}
	}

	private function timerEventHandler(event:TimerEvent):void
	{
		var start:int = timer.currentCount % petalsCount;
		var end:int = start + petalsCount + 1;
		for (var i:int = start; i < end; i++)
		{
			var index:int = (i < petalsCount) ? i : i - petalsCount;
			petals[index].alpha = 1 / (1 + end - i);
		}
	}

	private function drawPetal(g:Graphics, angle:Number):void
	{
		var p1:Point = Point.polar(radius, angle * RAD);
		var p2:Point = Point.polar(radius, (angle + petalGage / 2) * RAD);
		var p3:Point = Point.polar(innerRadius, angle * RAD);
		var p4:Point = Point.polar(radius, (angle - petalGage / 2) * RAD);

		g.moveTo(p1.x, p1.y);
		g.beginFill(color);
		g.curveTo(p2.x, p2.y, p3.x, p3.y);
		g.curveTo(p4.x, p4.y, p1.x, p1.y);
		g.endFill();
	}

}

Initial URL


Initial Description


Initial Title
Preloader Camomile

Initial Tags


Initial Language
ActionScript