used a few tutorials i found online and tried to rework to make a CountDown class for a project. Can’t figure out why it is wrong. For some reason the day counter is stuck at 48…
package {
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class CountDown extends Sprite {
private var now:Date;
private var end:Date = new Date(2009,0,31);
private var timer:Timer;
private var timeLeft:uint;
private var msTimer:Timer;
public var display:Counter = new Counter();
private var s:String;
private var m:String;
private var h:String;
private var d:String;
public function CountDown() {
init();
}
private function init():void {
timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, updateTimer, false, 0, true);
timer.start();
msTimer = new Timer(1);
msTimer.addEventListener(TimerEvent.TIMER, updateMS, false, 0, true);
msTimer.start();
addChild(display);
display.gradient_mc.mask = display.field;
display.msGradient_mc.mask = display.msField;
}
public function updateTimer(e:TimerEvent):void {
timeLeft = end.getTime() - now.getTime();
var seconds:uint = Math.floor(timeLeft / 1000);
var minutes:uint = Math.floor(seconds / 60);
var hours:uint = Math.floor(minutes / 60);
var days:uint = Math.floor(hours / 24);
seconds %= 60;
minutes %= 60;
hours %= 24;
s = seconds.toString();
if(s.length < 2) s = "0" + s;
m = minutes.toString();
if(m.length < 2) m = "0" + m;
h = hours.toString();
if(h.length < 2) h = "0" + h;
d = days.toString();
if(d.length < 2) d = "0" + d;
setTimeDisplay();
}
public function updateMS(e:TimerEvent):void {
now = new Date();
var ms:uint = Math.floor(now.getMilliseconds() / 10);
var msS:String = ms.toString();
if(msS.length < 2) msS = "0" + msS;
display.msField.text = msS;
}
public function setTimeDisplay() {
display.field.text = d + ":" + h + ":" + m + ":" + s;
}
}
}