0

我目前正在处理一个闪存程序,它在设定的时间内定期增加和减少一个变量。为此,我正在使用 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});

    }
4

3 回答 3

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
}
于 2013-07-09T12:23:40.593 回答
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}); 
}
于 2013-07-09T11:54:46.760 回答
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});

    }
于 2013-07-10T12:39:15.110 回答