I am making a circle countdown timer class. It works fine, however, the startAngle variable isn’t really working the way I wanted it to. It seems to start at 45 degrees even though I have it set to 0. I want it to start at the top and work it way around like a clock. So it would start at 12 and end at 12. Here is my class
// Create an instance of the class in an empty FLA
import com.ronnieswietek.utils.CircleTimer;
var counter:CircleTimer = new CircleTimer(15,0xff0000,0x000000);
counter.x = stage.stageWidth * 0.5;
counter.y = stage.stageHeight * 0.5;
addChild(counter);
// The class
package com.ronnieswietek.utils
{
import flash.display.*;
import flash.events.*;
import flash.utils.*;
import flash.text.*;
public class CircleTimer extends Sprite
{
private var timer :Timer;
private var circle :Sprite;
private var txt :TextField;
private var format :TextFormat;
private var duration :Number;
private var percent :Number;
private var color :uint;
private var textColor :uint;
public function CircleTimer(duration:Number = 20, color:uint = 0x000000, textColor:uint = 0xffffff)
{
addEventListener(Event.ADDED_TO_STAGE, initTimer);
this.duration = duration;
this.color = color;
this.textColor = textColor;
}
private function initTimer(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, initTimer);
txt = new TextField();
format = new TextFormat();
circle = new Sprite();
timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, updateTimer);
timer.start();
addChild(circle);
createTextField();
}
private function createTextField():void
{
format.color = textColor;
format.font = "Verdana";
txt.defaultTextFormat = format;
txt.text = String(duration);
txt.autoSize = TextFieldAutoSize.CENTER;
txt.x = 0 - (txt.width * 0.5);
txt.y = 0 - (txt.height * 0.5);
addChild(txt);
}
private function updateTimer(e:TimerEvent):void
{
percent = timer.currentCount / duration;
txt.text = String(duration - timer.currentCount);
txt.x = 0 - (txt.width * 0.5);
txt.y = 0 - (txt.height * 0.5);
if (timer.currentCount != duration)
{
circleLoader(circle,percent);
}
else
{
trace("end timer");
circleLoader(circle,percent);
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, updateTimer);
}
}
private function circleLoader(who:Sprite, pct:Number):void
{
who.graphics.clear();
var radius :Number = 25;
var endAngle :Number = (Math.PI * pct) * 2;
var startAngle :Number = 0;
if (endAngle != startAngle)
{
who.graphics.beginFill(color,100);
who.graphics.lineTo(radius,0);
circleSegmentTo(who,0,0,startAngle,endAngle,radius,1);
who.graphics.lineTo(0,0);
who.graphics.endFill();
}
}
private function circleSegmentTo(sp:Sprite, x:Number, y:Number, a1:Number, a2:Number, r:Number, dir:Number):void
{
var diff:Number = Math.abs(a2 - a1);
var divs:Number = Math.floor(diff / (Math.PI / 4)) + 1;
var span:Number = dir * diff / (2 * divs);
var rc :Number = r / Math.cos(span);
sp.graphics.moveTo(x + Math.cos(a1) * r,y + Math.sin(a1) * r);
for (var i:int = 0; i < divs; i++)
{
a2 = a1 + span;
a1 = a2 + span;
sp.graphics.curveTo(x + Math.cos(a2) * rc,y + Math.sin(a2) * rc,x + Math.cos(a1) * r,y + Math.sin(a1) * r);
}
}
}
}