Hi, sorry for my english but i'm italian. I'm trying to create a timer with as3 but it doesn't work. It will start from 15 minutes and it will arrive at zero, but it doensn't go!!! I'm not very able in as3 so anyone can tell me what's the problem?? The code
var endDateate = new Date(2008,6,20,14,30,0);
var countdownTimer:Timer = new Timer(1000);
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
countdownTimer.start();
function updateTime(e:TimerEvent):void
{
var nowate = new Date(2008,6,20,14,15,0);
var timeLeft:Number = endDate.getTime() - now.getTime();
var seconds:Number = Math.floor(timeLeft / 1000);
var minutes:Number = Math.floor(seconds / 60);
seconds %= 60;
minutes %= 60;
var sec:String = seconds.toString();
var min:String = minutes.toString();
Since you are creating "now" with hardcoded values, the difference between "now" and "endDate" will never change. You should derive "now" from the current time (and you should probably derive "endDate" from the current time as well.
Code:
var endTime:int = getTimer();
endTime += 15*60*1000; //adjust endTime to 15 minutes in the future.
var countdownTimer:Timer = new Timer(1000);
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
countdownTimer.start();
function updateTime(e:TimerEvent):void
{
var timeLeft:Number = endTime - getTimer();
var seconds:Number = Math.floor(timeLeft / 1000);
var minutes:Number = Math.floor(seconds / 60);
seconds %= 60;
minutes %= 60;
var sec:String = seconds.toString();
var min:String = minutes.toString();
if (sec.length < 2) {
sec = "0" + sec;
}
if (min.length < 2) {
min = "0" + min;
}
var time:String = min + ":" + sec;
time_txt.text = time;
}
Since you weren't actually using any of the fancy features of Date, I replaced it with getTimer.
Thank you!!!It works perfectly in a project in which i paste the code in the first frame... But if i insert the code in a project in which i use the object oriented programming and i don't insert the code in the frame but in the classes, it give me the errors below:
1180: Call to a possibly undefined method getTimer.
If this is a non-document class, then the problem is that it has no idea what time_txt is. You can create and add it within the class, or you can pass it to the class when you instantiate it. Here's the second way, which is a bit more flexible:
Code:
package{
//imports here
public class CountDown extends Sprite{
private var endTime:int;
private var time_txt:TextField;
public function CountDown(tt:TextField):void{
time_txt = tt;
go();
}
public function go():void{
endTime = getTimer();
endTime += 15*60*1000; //adjust endTime to 15 minutes in the future.
var countdownTimer:Timer = new Timer(1000);
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
countdownTimer.start();
}
function updateTime(e:TimerEvent):void
{
var timeLeft:Number = endTime - getTimer();
var seconds:Number = Math.floor(timeLeft / 1000);
var minutes:Number = Math.floor(seconds / 60);
seconds %= 60;
minutes %= 60;
var sec:String = seconds.toString();
var min:String = minutes.toString();
if (sec.length < 2) {
sec = "0" + sec;
}
if (min.length < 2) {
min = "0" + min;
}
var time:String = min + ":" + sec;
time_txt.text = time;
}
}//end class
}//end package
And you'd instantiate it like this in your document:
Code:
var countdown:CountDown = new CountDown(time_txt); //where time_txt is a TextField on the stage
addChild(countdown);
Yes it doesn't give me any errors, but i don't see the timer on the display... I have put a trace after the addChild and i see on the output panel the written! Ohh i don't understeand why!I really thank u...
var time_txt2:TextField = new TextField();
time_txt2.x = 100;
time_txt2.y = 100;
addChild(time_txt2);
var countdown:CountDown = new CountDown(time_txt2);
When it's implemented as above, CountDown does not itself have any visual appearance, so it does not need to extend Sprite. I've removed the addChild(countdown) line because it doesn't really do anything useful.
Yes it is a dynamic Text on the stage! i try with time_txt2 but another time i don't see the timer on the stage... Noooooo. I send you the directory with the project (onli the as files.)..if u want u can take a look at it.
However rhank u!
It's just an expression. I was asking any of the other people on the board who may have read this thread whether they'd like to add anything to the discussion.
To reset the timer, you'll have to keep an accessible reference to it around. Right now, it's declared as a local variable in the go function, which means you can't easily do anything with it later. You can move it up to the class level and add a reset like this:
Code:
public class CountDown {
private var endTime:int;
private var time_txt:TextField;
private var countdownTimer:Timer;
public function CountDown(tt:TextField):void{
time_txt = tt;
countdownTimer:Timer = new Timer(1000);
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
go();
}
public function go():void{
countdownTimer.reset();
endTime = getTimer();
endTime += 15*60*1000; //adjust endTime to 15 minutes in the future.
countdownTimer.start();
}
function updateTime(e:TimerEvent):void
{
var timeLeft:Number = endTime - getTimer();
var seconds:Number = Math.floor(timeLeft / 1000);
var minutes:Number = Math.floor(seconds / 60);
seconds %= 60;
minutes %= 60;
var sec:String = seconds.toString();
var min:String = minutes.toString();
if (sec.length < 2) {
sec = "0" + sec;
}
if (min.length < 2) {
min = "0" + min;
}
var time:String = min + ":" + sec;
time_txt.text = time;
}
/**
* resets and restarts the internal timer.
*/
public function reset():void{
go();
}
}//end class
I've re-arranged things again, so that go() can be used both for the initial start and to restart later. I've added a public restart method so that you can call countdown.restart() from outside the class, but you could just do it with go(). I've removed "extends Sprite" because it doesn't need to be a Sprite. Do not try to add a CountDown to the stage now.
I try with that code..and the timer restart but the problem is that the other timer remain in memory, so i have at the same time two timer...how can i free the memory??