views:

37

answers:

3

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,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


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)

A: 

why not setting the Timer.repeatCount to 3300000 ?

kajyr
Please explain. Wouldn't that just repeat after 33 thousand seconds?
VideoDnd
no. that means that the timer will tick 33 thousands times. The first parameter in the constructor is the time between two ticks
kajyr
+2  A: 

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.)

Heath Hunnicutt
I'm still learning. How would I use with var countValue:int = 3300000;
VideoDnd
fcount = Math.abs(count); that worked, thanks.
VideoDnd
+1  A: 

In your code simply change:

var fraction:int = Math.abs(i % 100); 
var whole:int = Math.abs(i / 100);

("0000000" + whole).substr(-**5**, 7)
Oliver
("0000000" + whole).substr(-**7**, 7) is fine , I just saw you wanted the other two 0 there
Oliver