Liam Barrett
Liam Barrett

Reputation: 45

actionscript - how to always display numbers in XXXX format

I am currently dealing with a flash program that periodically increases and decreases a variable by a set amount within a set time. To do this i am using TweenLite. My problem right now is that:

I want the number to always be 6 numbers long. So, the number 10 would be displayed as 000010. 156 would be 000156, and so on. My program as current simply displays the 10 or 156.

How do i make it so a specific text box is always to XXXXXX format?

the reason my situation is slightly different than most is because my numbers are incrementing a lot, which means that they change from X to XX to XXX fluidly, so simply sticking 0000 ahead of my digit isnt a valid response to this problem as there will technically be an extra 0 thrown into mix when the digit changes from X to XX, etc.

I currently have the following code in my flash program :

import com.greensock.*;
import com.greensock.easing.*;

var score:Number = 0;             // starting value
var targetScore:Number = 0;            // the value that the starting value will change
                                       // to (it increases before *score* increases)

function showScore():void{
    score_mc.score_txt.text = int(score);         //display score in my text box
    }

button_btn.addEventListener(MouseEvent.CLICK, increaseScore);

function increaseScore(e:MouseEvent):void{
    targetScore+=10;                                 //increase targetScore before score
    TweenLite.to(this, 1, {score:targetScore, onUpdate:showScore, ease:Linear.easeNone}); 
                           // increase score to the same value as target score, 
                           // over 1 second. The Linear.ease bit simply makes it 
                           // a constant change from one number to the next.
    }

EDIT : this seems to work perfectly (takes into consideration original format of text too :

import com.greensock.*;
import com.greensock.easing.*;

var score:Number = 0;
var targetScore:Number = 0;  

function showScore():void{
    score_mc.score_txt.defaultTextFormat = score_mc.score_txt.getTextFormat();
    if (score < 10){
        score_mc.score_txt.text = "00000" + int(score);
    } else if (score < 100) {
            score_mc.score_txt.text = "0000" + int(score);
        } else if (score < 1000) {
            score_mc.score_txt.text = "000" + int(score);
        } else {
            score_mc.score_txt.text = "00" + int(score);
        }

}

button_btn.addEventListener(MouseEvent.CLICK, increaseScore);
function increaseScore(e:MouseEvent):void{
    targetScore+=900;
    TweenLite.to(this, 2, {score:targetScore, onUpdate:showScore, ease:Linear.easeNone});

    }

Upvotes: 0

Views: 548

Answers (3)

Liam Barrett
Liam Barrett

Reputation: 45

This seems to do the job but others have left answers that may be more practical and computer friendly,

import com.greensock.*;
import com.greensock.easing.*;

var score:Number = 0;
var targetScore:Number = 0;

function showScore():void{
    if (score < 10){
        score_mc.score_txt.text = "000" + int(score);
    } else if (score < 100) {
            score_mc.score_txt.text = "00" + int(score);
        } else if (score < 1000) {
            score_mc.score_txt.text = "0" + int(score);
        }

}

button_btn.addEventListener(MouseEvent.CLICK, increaseScore);
function increaseScore(e:MouseEvent):void{
    targetScore+=50;
    TweenLite.to(this, 2, {score:targetScore, onUpdate:showScore, ease:Linear.easeNone});

    }

Upvotes: 0

divillysausages
divillysausages

Reputation: 8033

Something like this should work:

public function printScore( score:int ):String
{
    var str:String = "00000" + score; // add the max leading zeros to your number
    return str.substr( str.length - 6 ); // return only the last 6 chars
}

Upvotes: 1

jfgi
jfgi

Reputation: 107

You can try to modify your code a bit:

function showScore(newScore:String):void{
    score_mc.score_txt.text = newScore;         //display score in my text box
}

And also tweenlite code:

function increaseScore(e:MouseEvent):void{
   targetScore+=10;                                 //increase targetScore before score
   var newScore = targetScore.toString("000000");
   TweenLite.to(this, 1, {score:targetScore, onUpdate:showScore, onUpdateParams:[newScore], ease:Linear.easeNone}); 
}

Upvotes: 0

Related Questions