我目前正在处理一个闪存程序,它会在设定的时间内定期增加和减少一个变量。要做到这一点,我使用TweenLite。我现在的问题是:
我希望这个数字总是6个数字。因此,数字10将显示为000010. 156将为000156,依此类推。我的程序当前只显示10或156.
如何制作,以便特定文本框始终为XXXXXX格式?
我的情况与大多数情况略有不同的原因是因为我的数字增加很多,这意味着它们从X变为XX流动,所以简单地在我的数字之前坚持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});
}
答案 0 :(得分:1)
这样的事情应该有效:
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
}
答案 1 :(得分:0)
您可以尝试稍微修改一下代码:
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});
}
答案 2 :(得分:0)
这似乎可以完成这项工作,但其他人已经留下了可能更实用且更适合计算机的答案,
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});
}