Flash actionscript-如何始终以XXXX格式显示数字

Flash actionscript-如何始终以XXXX格式显示数字,flash,actionscript,numbers,format,digits,Flash,Actionscript,Numbers,Format,Digits,我目前正在处理一个flash程序,它在一个设定的时间内周期性地增减一个变量。为此,我使用TweenLite。我现在的问题是: 我希望数字的长度始终为6个数字。因此,数字10将显示为000010。156等于000156,依此类推。我的当前程序只显示10或156 如何使特定文本框始终为XXXXXX格式 我的情况与大多数情况稍有不同的原因是因为我的数字在大量增加,这意味着它们从X到XX流畅地变化到XXX,所以简单地在我的数字前面加上0000并不是解决这个问题的有效方法,因为从技术上讲,当数字从X变为X

我目前正在处理一个flash程序,它在一个设定的时间内周期性地增减一个变量。为此,我使用TweenLite。我现在的问题是:

我希望数字的长度始终为6个数字。因此,数字10将显示为000010。156等于000156,依此类推。我的当前程序只显示10或156

如何使特定文本框始终为XXXXXX格式

我的情况与大多数情况稍有不同的原因是因为我的数字在大量增加,这意味着它们从X到XX流畅地变化到XXX,所以简单地在我的数字前面加上0000并不是解决这个问题的有效方法,因为从技术上讲,当数字从X变为XX时,会在混合中加入额外的0,以此类推

我的flash程序中目前有以下代码:

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.
    }
编辑:这似乎工作得很好(也考虑了文本的原始格式:

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

    }
import com.greensock.*;
导入com.greensock.com;
var得分:数字=0;
var targetScore:数值=0;
函数showScore():void{
score_mc.score_txt.defaultTextFormat=score_mc.score_txt.getTextFormat();
如果(分数<10){
score\u mc.score\u txt.text=“00000”+int(分数);
}否则如果(分数<100){
score\u mc.score\u txt.text=“0000”+int(分数);
}否则如果(分数<1000){
score\u mc.score\u txt.text=“000”+int(分数);
}否则{
score\u mc.score\u txt.text=“00”+int(分数);
}
}
按钮添加事件列表器(MouseEvent.CLICK,增加分数);
函数递增分数(e:MouseeEvent):无效{
targetScore+=900;
to(this,2,{score:targetScore,onUpdate:showScore,ease:Linear.easeNone});
}

您可以尝试稍微修改代码:

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

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

像这样的方法应该会奏效:

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
}

这似乎很管用,但其他人留下的答案可能更实用,更便于计算机操作

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

    }
import com.greensock.*;
导入com.greensock.com;
var得分:数字=0;
var targetScore:数值=0;
函数showScore():void{
如果(分数<10){
score\u mc.score\u txt.text=“000”+int(分数);
}否则如果(分数<100){
score\u mc.score\u txt.text=“00”+int(分数);
}否则如果(分数<1000){
score\u mc.score\u txt.text=“0”+int(分数);
}
}
按钮添加事件列表器(MouseEvent.CLICK,增加分数);
函数递增分数(e:MouseeEvent):无效{
targetScore+=50;
to(this,2,{score:targetScore,onUpdate:showScore,ease:Linear.easeNone});
}

ok,我得到了以下错误:RangeError:error#1003:基数参数必须介于2和36之间;得到0。在Number$/\u toString()在Number/at Panel_fla::main timeline/increaseScore()处我试着看看我现在能不能找出它的原因。令人沮丧的是,我找到了如何以XX.XXXX格式生成数字的方法,但仍然无法做到这一点>function showScore():void{score\u mc.score\u txt.text=score.toString(“0000000”);//在我的文本框中显示分数}ok,它是000000,即使我将它增加到2,也只会导致问题。我认为,因为我将值更改为int,并在这里将其称为字符串,这会导致问题。听起来对吗?对不起,我以前用过flash,但从来没有用过这样的方式,flashlite让我进入了一个新的未知领域:p。好的,我现在就测试一下,但我很确定我已经尝试过了(一直在尽我所能地使用toString:p)你可以看看这个。也许在你的代码中也可以这样做,我把它放进了我上面提到的代码中,当然,在其他函数之外。没有什么不同于b4的事情发生,仍然在计算,但没有数字变化。不过,谢谢你,这是另一件要尝试和使用的事情-beats没有任何想法。我也尝试过将函数的内容放入showScore,但没有效果。我尝试的这种方法没有任何效果。我确信我不能玩字符串,因为这会不断累积错误。如果我跟踪str,它永远不会改变(当程序运行时,我让str远离我改变的实际数字),你会得到什么错误?您应该能够像
score\u mc.score\u txt.text=printScore(score)那样使用它