views:

251

answers:

3

This is my 2-part question. My understanding is small so "beer" with me.

This example counts from 1-100. I want the decimal keep counting to ten over-and over. (a.)How do I loop the decimal? 1-10 over-and-over. (b.)How to I get the whole and decimal value on the same dynamic text field?

alt text

THING
"It's a counter with whole and decimal values"
-text filed receive string of whole number and decimal place
-decimal fast, whole number slow

CODE

/*I made two text fields, change the names and values, but the values didn't
respond properly. 1000 = 1 sec and 100 = 1/10th or a second. The whole thing changed.*/
//working code

var timer:Timer = new Timer(1000, 100);
timer.addEventListener(TimerEvent.TIMER, countdown);
timer.start();
function countdown(event:TimerEvent) {
    myText.text = String(0 + timer.currentCount);
}

IDEAS
(a.)
//RESET START
function countdown($evt:TimerEvent):void {
timer.reset();
timer.start();

VERBOSE EXAMPLE
if Number's less than 11, do it again

(b.)
//STRING
var time:String =whole + ":" + decimal;

time_txt.text = countdown;

UNEXPECTED PROBLEMS
-changing values (1000, 100) didn't work "affects the other number"
-I don't know how I'm going to add velocity to something in the timer class "an other post"

alt text

THE DECIMALS STAY IN THEIR PLACE

//CA, NC, LONDON
var timer:Timer = new Timer(10);  
var count:int = 0; //start at -1 if you want the first decimal to be 0  
var fcount:int = 0; 

timer.addEventListener(TimerEvent.TIMER, incrementCounter);  
timer.start();  


function incrementCounter(event:TimerEvent) { 
  count++; 

  var whole_value:int = int(count / 100); //change value
  var tenths:int = int(count / 10) % 10;  
  var hundredths:int = int(count) % 10;  

  mytext.text = whole_value + " : " + tenths + hundredths; 
}

CRITERIA "I forgot to include 2/10/10"
WHOLES
X 10 multiples

DECIMALS
tenths dec = 10% of ones
hundreds dec = 10% of tenths dec

"thanks for the help everyone"

+4  A: 

It's probably better to use your own count variable instead of the one from the timer. You want something like the following. Every time the timer ticks the count will update by one. The "%" is the modulo operator.

var timer:Timer = new Timer(1000);
var count:int = 0; //start at -1 if you want the first decimal to be 0

timer.addEventListener(TimerEvent.TIMER, incrementCounter);
timer.start();


function incrementCounter(event:TimerEvent) {
  count++;

  var whole_value:int = int(count / 10);
  var decimal_value:int = count % 10; 
  mytext.text = whole_value + " : " + decimal_value;
}
Nathan Villaescusa
thanks! same comment "I added a hundred decimal, but want to limit the decimals to 2-places for appearance. Is there a way to control it so I have a decimal of 10th and one of 100ths looping "like a casino display"
VideoDnd
+1  A: 
var timer:Timer = new Timer(100);
timer.addEventListener(TimerEvent.TIMER, countdown);
timer.start();
var time = 2000;
function countdown(event:TimerEvent) {
 time -= 1;
 var seconds = int(time/10);
 var tenths = int(time - seconds*10);
 time_txt.text = seconds + " : " + tenths;
    if (time == 0) {
        timer.stop();
    }
}
sberry2A
added "var huns = int(time - seconds*100);" Is there any way to constrain the decimal to 2-places and have the one to the right move faster?
VideoDnd
+2  A: 

To adapt Nathan's answer for 100/ths you could do something like this:

function incrementCounter(event:TimerEvent) {
  count++;

  var whole_value:int = int(count / 100);
  var tenths:int = int(count / 10) % 10; 
  var hundredths:int = int(count) % 10; 

  mytext.text = whole_value + " : " + tenths + hundredths;
}
Richard Inglis
that's much better. My decimals stay in their place now.
VideoDnd