Countdown Preloader
Inspired by this : http://wonderfl.net/c/gqjs
But, I want opened circle rather than closed circle.
Drawing algorithm taken from : http://www.flexstreamer.com/?p=106
/**
* Copyright hidrodixtion2 ( http://wonderfl.net/user/hidrodixtion2 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/sGS0
*/
package {
import flash.display.Sprite;
import flash.events.*;
import com.bit101.components.*;
public class CountPre extends Sprite {
private var _iptTime:NumericStepper;
private var _btnCloseCirc:PushButton, _btnOpenCirc:PushButton;
private var _type:String;
private var _factor:Number;
private var _percent:Number = 0;
private var _sprite:Sprite;
public function CountPre()
{
var lblTime:Label = new Label(this, 2, 2, "Time :");
_iptTime = new NumericStepper(this, lblTime.x + lblTime.width + 6, lblTime.y);
_iptTime.value = 5;
_iptTime.minimum = 1;
_btnCloseCirc = new PushButton(this, _iptTime.x + _iptTime.width + 6, lblTime.y, "Close Circle", onBtnClick);
_btnOpenCirc = new PushButton(this, _btnCloseCirc.x + _btnCloseCirc.width + 6, lblTime.y, "Open Circle", onBtnClick);
_sprite = new Sprite();
_sprite.x = stage.stageWidth/2;
_sprite.y = stage.stageHeight/2;
_sprite.rotation = -90;
addChild(_sprite);
}
private function onBtnClick(e:MouseEvent):void
{
switch (e.target)
{
case _btnCloseCirc:
_type = "close";
break;
case _btnOpenCirc:
_type = "open";
break;
}
disableButton();
_factor = (360/_iptTime.value)/stage.frameRate;
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function disableButton():void
{
_btnCloseCirc.removeEventListener(MouseEvent.CLICK, onBtnClick);
_btnOpenCirc.removeEventListener(MouseEvent.CLICK, onBtnClick);
}
private function enableButton():void
{
_btnCloseCirc.addEventListener(MouseEvent.CLICK, onBtnClick);
_btnOpenCirc.addEventListener(MouseEvent.CLICK, onBtnClick);
}
private function onEnterFrame(e:Event):void
{
if (_percent <= 360)
{
if (_type.toLowerCase() == "open")
drawSegment(_sprite, _percent, 360, 120, 0, 0, 0xff9933);
else if (_type.toLowerCase() == "close")
drawSegment(_sprite, 0, _percent, 120, 0, 0, 0xff9933);
_percent += _factor;
}
else
{
_percent = 0;
this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
enableButton();
}
}
private function drawSegment(holder:Sprite, startAngle:Number, endAngle:Number, segmentRadius:Number, xpos:Number, ypos:Number, fillColor:Number, step:Number = 0.5):void
{
holder.graphics.clear();
holder.graphics.beginFill(fillColor);
var degreesPerRadian:Number = Math.PI / 180;
startAngle *= degreesPerRadian;
endAngle *= degreesPerRadian;
step *= degreesPerRadian;
// Draw the segment
holder.graphics.moveTo(xpos, ypos);
for (var theta:Number = startAngle; theta < endAngle; theta += Math.min(step, endAngle - theta))
{
holder.graphics.lineTo(xpos + segmentRadius * Math.cos(theta), ypos + segmentRadius * Math.sin(theta));
}
holder.graphics.lineTo(xpos + segmentRadius * Math.cos(endAngle), ypos + segmentRadius * Math.sin(endAngle));
holder.graphics.lineTo(xpos, ypos);
}
}
}