Copy link to clipboard
Copied
package {
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.utils.Timer;
import flash.text.TextFormat;
public class timer2 extends MovieClip {
private var textfield:TextField;
public function timer2():void {
var count:uint = 60;
var myTimer:Timer = new Timer(1000, count);
myTimer.addEventListener(TimerEvent.TIMER, countdown, false, 0, true);
myTimer.start();
textfield = new TextField();
textfield.defaultTextFormat = new TextFormat("Arial", 24, 0xff0000, true);
textfield.x = 30;
textfield.y = 60;
textfield.text = String(count);
addChild(textfield);
}
private function countdown(e:TimerEvent):void {
textfield.text = String(parseInt(textfield.text) - 1);
}
}
}
package {...
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
public class Timer2 extends Sprite {
private var textfield:TextField;
private var count:uint = 120;
public function Timer2():void {
var myTimer:Timer = new Timer(1000, count);
myTimer.addEventListener(TimerEvent.TIMER, count
Copy link to clipboard
Copied
:
package {
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.utils.Timer;
import flash.text.TextFormat;
public class timer2 extends MovieClip {
private var textfield:TextField;
public function timer2():void {
var count:uint = 120;
var myTimer:Timer = new Timer(1000, count);
myTimer.addEventListener(TimerEvent.TIMER, countdown, false, 0, true);
myTimer.start();
textfield = new TextField();
textfield.defaultTextFormat = new TextFormat("Arial", 24, 0xff0000, true);
textfield.x = 30;
textfield.y = 60;
textfield.text = formatS(String(count));
addChild(textfield);
}
private function countdown(e:TimerEvent):void {
textfield.text = formatS(String(parseInt(textfield.text) - 1));
}
private function formatS(s:String):String{
var n:int = int(s);
var min:int = Math.floor(n/60);
var sec:int = n-min*60;
var minS:String=String(min);
var secS:String=String(sec);
while(minS.length<2){
minS="0"+minS;
}
while(secS.length<2){
secS="0"+secS;
}
return minS+":"+secS;
}
}
}
Copy link to clipboard
Copied
Something is wrong with the code you gave the timer starts at 2:00 then as it counts it restarts from 0:01 then -1:59 and stuck up at -1:58?
Copy link to clipboard
Copied
this line:
textfield.text = formatS(String(parseInt(textfield.text) - 1));
should be:
textfield.text=formatS(String(120-e.target.currentCount));
Copy link to clipboard
Copied
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
public class Timer2 extends Sprite {
private var textfield:TextField;
private var count:uint = 120;
public function Timer2():void {
var myTimer:Timer = new Timer(1000, count);
myTimer.addEventListener(TimerEvent.TIMER, countdown, false, 0, true);
myTimer.start();
textfield = new TextField();
textfield.defaultTextFormat = new TextFormat("Arial", 24, 0xff0000, true);
textfield.x = 30;
textfield.y = 60;
textfield.text = format(count);
addChild(textfield);
}
private function countdown(e:TimerEvent):void {
textfield.text = format(--count);
}
private function format(n:uint):String {
return Math.floor(n/60) + ":" + (n % 60 < 10 ? "0" + n % 60 : n % 60);
}
}
}
Copy link to clipboard
Copied
tnx to all of you!!
Copy link to clipboard
Copied
you're welcome.
p.s. in the future, please mark helpful/correct answers.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now