2010-02-10 6 views
0

Comment faire un espace réservé de zéro pour le compteur?"espace réservé zéro" pour le compteur de travail as3

Ma pensée était une condition "si" avec une plus grande et moins pourrait fonctionner.

if count is more than .00 and less than 10.00, add "0000000" etc. 

alt text http://www.ashcraftband.com/myspace/videodnd/icon_3.jpg

CODE "de certains des meilleurs esprits"

//counts 
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; 
} 

/////////////////////////////////////////////// 

//counts and accelerates 
//CA, NC, LONDON "increments" 
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++; 
    // 
    fcount=int(count*count/10000);//starts out slow... then speeds up 
    // 
    var whole_value:int = int(fcount/100); //change value 
    var tenths:int = int(fcount/10) % 10; 
    var hundredths:int = int(fcount) % 10; 

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

alt text http://www.ashcraftband.com/myspace/videodnd/icon-3.jpg

I fait des zéros ... eeeeerrrrrrr ... pas animation, ne pas rire! malédictions

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++; 
    // 
    fcount=int(count*count/10000);//starts out slow... then speeds up 
    // 
    var whole_value:int = int(fcount/100); //change value 
    var tenths:int = int(fcount/10) % 10; 
    var hundredths:int = int(fcount) % 10; 
////////////// 
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; 
} 

function test():void { 
    for (var i:int = 1; i<100000; i += 3) { 
     trace(i + " -> " + formatCount(i)); 
    } 
} 
////////////// 
mytext.text = formatCount(whole_value + " : " + tenths + hundredths); 

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

"Merci pour l'aide les gars"

Répondre

1

Voici une fonction pour le formater comme vous le souhaitez.

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; 
} 

function test():void { 
    for (var i:int = 1; i<100000; i += 3) { 
     trace(i + " -> " + formatCount(i)); 
    } 
} 
]]> 

sortie de l'échantillon (espacement ajouté):

1  -> 0000000.01 
4  -> 0000000.04 
7  -> 0000000.07 
10 -> 0000000.10 
13 -> 0000000.13 
97 -> 0000000.97 
100 -> 0000001.00 
103 -> 0000001.03 
235 -> 0000002.35 
520 -> 0000005.20 
997 -> 0000009.97 
1000 -> 0000010.00 
1003 -> 0000010.03 
99997 -> 0000999.97 
+0

Belle solution Sam! –

+0

Je ne sais pas comment l'appliquer, presque hors de noobsville. La sortie n'affiche aucune erreur, mais n'affiche pas. Probablement moi. –

+1

Vous feriez quelque chose comme: 'myTextBox.text = formatCount (score);' –

0

Pour autant que je sais que vous devez faire vous-même:

Voici quelques code.

0

Voici un exemple rapide, c'est assez simple, mais laissez-moi savoir si vous avez des questions.

var n:Number = 999.99123; 
var minLength:int = 15; 
var s:String = n.toFixed(2); 
var diff:int = minLength - s.length; 

while (diff > 0) { 
    s = '0' + s; 
    diff--; 
} 

trace(s); 

EDIT: avez-vous voulu "dixièmes" pour être toujours 0?

+0

Je l'ai testé ce code et il n'a pas produit les résultats du OP demandé. J'ai obtenu des résultats comme '3 -> 000000000003.00' et' 153 -> 000000000153.00' mais il devrait être '3 -> 0000000.03' et' 153 -> 0000001.53' –

+0

Cet exemple mettra en forme un nombre (ie virgule flottante) à 2 décimales ... cependant la variable count de VideoDnd est un entier (ce qui équivaut à 1/100ème de seconde). Vous pouvez corriger cela en changeant la première ligne à: var n: Number = count/100; –

+0

des idées? Je suis coincé. –

0

MILLIONS COMPTEUR AVEC ZERO PLACEHOLDERS

//CA, NC, LONDON, ED, GA "increments" 
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++; 
    fcount=int(count*count/10000);//starts out slow... then speeds up 
    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); 
} 
Questions connexes