set countdown correctly, as3
How can I set my countdown correctly?
I'm counting from 33,000.00 to zero. It works in a fashion, but the minus operator appears in the textfield.
//Countdown from 33开发者_如何学Go,000.00 to zero
var timer:Timer = new Timer(10);
var count:int = -3300000;
var fcount:int = 0;
timer.addEventListener(TimerEvent.TIMER, incrementCounter);
timer.start();
function incrementCounter(event:TimerEvent) {
count++;
fcount=int(count);
mytext.text = formatCount(fcount);
}
function formatCount(i:int):String {
var fraction:int = i % 100;
var whole:int = i / 100;
return ("0000000" + whole).substr(-7, 7) + "." + (fraction < 10 ? "0" + fraction : fraction);
}
alt text http://www.ashcraftband.com/myspace/videodnd/nm___.jpg
EXAMPLE
I need something I can update with XML, to be an up-counter or down-counter depending on the variables.//Count up from 33,000.00
var countValue:int = 3300000;
count = countValue;
//Count down from 33,000.00
var countValue:int = -3300000;
count = countValue;
This is all I needed
fcount = Math.abs(count)To get rid of the minus sign:
// Absolute value of i will be calculated in abs_i.
var abs_i:int = i;
if (abs_i < 0)
abs_i = -abs_i;
var fraction:int = abs_i % 100;
var whole:int = abs_i / 100;
To handle the case where you wish to count up, you will have to do things a bit differently. It would be better to have two functions, including a new function named decrementCounter. If you like, there can be an event handler which uses an if (counter < 0)
to determine which should be called.
EDIT: On re-reading your code it seems you intended
fcount=int(count)
might solve your problem, but you could call
fcount = Math.abs(count)
and then your "formatted count" would always be a positive value. Then you could ignore the changes I recommended originally, above.
(You don't need to call int()
because count
is already of type int
, as is fcount
.)
why not setting the Timer.repeatCount to 3300000 ?
In your code simply change:
var fraction:int = Math.abs(i % 100);
var whole:int = Math.abs(i / 100);
("0000000" + whole).substr(-**5**, 7)
精彩评论