我目前正在处理一个闪存程序,它在设定的时间内定期增加和减少一个变量。为此,我正在使用 TweenLite。我现在的问题是:
我希望数字始终为 6 个数字。因此,数字 10 将显示为 000010。156 将显示为 000156,依此类推。我的当前程序仅显示 10 或 156。
我如何使特定的文本框始终为 XXXXXX 格式?
我的情况与大多数情况略有不同的原因是因为我的数字增加了很多,这意味着它们从 X 到 XX 到 XXX 流畅地变化,所以简单地将 0000 放在我的数字前面并不是对这个问题的有效回应,因为从技术上讲当数字从 X 变为 XX 等时,是一个额外的 0。
我目前在我的闪存程序中有以下代码:
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});
}