Tuesday, February 15, 2011

Create a nice score displaying effect using AS3: Way 2

In this tutorial we'll create a nice score displaying effect in AS3, a modified version of this.

It is going to look like this:


Here's the code:

var realNumber:Number = 25238;
addZeros();

setInterval(updateDisplayedScore, 10);
var displayedNumber:Number = 0;

function updateDisplayedScore():void
{
displayedNumber += Math.round((realNumber-displayedNumber)/5);
if (realNumber - displayedNumber < 5 && realNumber - displayedNumber > -5)
{
displayedNumber = realNumber;
}
addZeros();
}

function addZeros():void
{
var str:String = displayedNumber.toString();
scoreDisplay.text = "$";
for (var i=0; i < (6 - str.length); i++)
{
scoreDisplay.appendText("0");
}
scoreDisplay.appendText(str);
}

As you can see, the updateDisplayedScore function is much more shorter than the one used in my previous score displaying tutorial. It also looks better.

Just like before, we have 2 variables - realNumber and displayedNumber, a textfield called scoreDisplay, a function updateDisplayedScore that is called every 10 milliseconds and a function addZeros that manages the zeros in front of the displayed number.

Thanks for reading!

Related:

Create a nice score displaying effect: Way 1

No comments:

Post a Comment