Skip to main content
Inspiring
November 1, 2011
Answered

How to make score start from 0000(4 digit) instead of 0?

  • November 1, 2011
  • 2 replies
  • 1206 views

I use:

////////////////////////////////////////////////////////////////////////

var gameScoreField:TextField = new TextField();

var gameScore:uint = 0;

gameScoreField.text = "Score: " + gameScore ;

addChild(gameScoreField);

/////////////////////////////////////////////////////////////////////////////////////

If i use var gameScore:uint = 0000; the result will still return one 0, what can i do to make it 0000 instead?

This topic has been closed for replies.
Correct answer Joe ... Ward

You store the score as a number in a variable. You convert it to a string in order to display it.

while( scoreDisplay.length < 4 ) nnnn; is a loop which will repeat while the length of the string is less than 4. In this case, the program adds a "0" to the front of the scoreDisplay string. It then checks the length again, adds another "0", etc, until the length is 4.

The way I would use this would be to create a updateScore() function that adds a value to the score and then converts it to a padded string:

var gameScoreField:TextField = new TextField();

var gameScore:uint = 0;

var gameScoreDisplay:String = "0000";

gameScoreField.text = "Score: 0000";

addChild(gameScoreField);

function updateScore( amount:int ):void

{

     gameScore += amount;

     if( gameScore > 9999 ) gameScore = 9999;

     if( gameScore < 0 ) gameScore = 0;

     gameScoreDisplay= gameScore.toString();

     while( gameScoreDisplay.length < 4 ) gameScoreDisplay = "0" + gameScoreDisplay;

     gameScoreField.text = "Score: " + gameScoreDisplay;

}

Then you could call this function whenever you wanted to update the score. Make sense?

2 replies

Participant
November 1, 2011

You can use a String instead of uint.

var gameScore:uint = 0;
gameScoreField.text = getGameScore(gameScore);

//if gameScore=0,   String(gameScore).length = 1
function getGameScore(i:uint):String
{
switch (String(i).length)
{
  case 1 :
   return "000"+i;
   break;
  case 2 :
   return "00"+i;
   break;
  case 3 :
   return "0"+i;
   break;
  default :
   return String(i);
   break;
}
}

Participating Frequently
November 1, 2011

Use a string and then pad it with 0's if needed. A simpleminded way to do it is this:

var score:uint = 0;

var scoreDisplay:String = score.toString();

while( scoreDisplay.length < 4 ) scoreDisplay = "0" + scoreDisplay;

trace( scoreDisplay ); //prints: 0000

Inspiring
November 1, 2011

can you explain how does this line works"

while( scoreDisplay.length < 4 ) scoreDisplay = "0" + scoreDisplay;

and since it is converted to text and how do I increase the score when a hit test is triggered? The least is 0 and highest is 9999..

Joe ... WardCorrect answer
Participating Frequently
November 1, 2011

You store the score as a number in a variable. You convert it to a string in order to display it.

while( scoreDisplay.length < 4 ) nnnn; is a loop which will repeat while the length of the string is less than 4. In this case, the program adds a "0" to the front of the scoreDisplay string. It then checks the length again, adds another "0", etc, until the length is 4.

The way I would use this would be to create a updateScore() function that adds a value to the score and then converts it to a padded string:

var gameScoreField:TextField = new TextField();

var gameScore:uint = 0;

var gameScoreDisplay:String = "0000";

gameScoreField.text = "Score: 0000";

addChild(gameScoreField);

function updateScore( amount:int ):void

{

     gameScore += amount;

     if( gameScore > 9999 ) gameScore = 9999;

     if( gameScore < 0 ) gameScore = 0;

     gameScoreDisplay= gameScore.toString();

     while( gameScoreDisplay.length < 4 ) gameScoreDisplay = "0" + gameScoreDisplay;

     gameScoreField.text = "Score: " + gameScoreDisplay;

}

Then you could call this function whenever you wanted to update the score. Make sense?