Search code examples
actionscript-3flash-cs6dynamic-text

AS3-Flash cs6 How to make numbers have a comma?


I am making a game that when you click on the Monster your score gets +1. But when your score goes over 1000 I would like it like this 1,000 rather than 1000. I am not sure how to do this as I have not learnt much action script. I have embed number and punctuation into the font. Here is my code so far:

var score:Number = 0;

Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

Monster.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler);

function fl_TapHandler(event:TouchEvent):void
{
    score = score + 1;
    Taps_txt.text = (score).toString();
}

Help will greatly appreciated.


Solution

  • To show the score with comma, you can do like this : ( comments are inside the code )

    var score:Number = 0;
    var score_str:String;
    var score_str_len:int;
    
    Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
    
    Monster.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler);
    
    function fl_TapHandler(event:TouchEvent):void
    {
        score = score + 1;
    
        score_str = score.toString();
        score_str_len = score_str.length;
    
        // here you can use score > 999 instead of score_str_len > 3
        Taps_txt.text = 
            score_str_len > 3 
    
            // example : 1780
            // score_str_len                        = 4
            // score_str.substr(0, 4 - 3)           = 1     : get thousands
            // score_str.substr(4 - 3)              = 780   : get the rest of our number : hundreds, tens and units
            // => 1 + ',' + 780                     = 1,780 : concatenate thousands + comma + (hundreds, tens and units)
            ? score_str.substr(0, score_str_len-3) + ',' + score_str.substr(score_str_len-3) 
            : score_str
        ;
        // gives :
        // score == 300   => 300
        // score == 1285  => 1,285
        // score == 87903 => 87,903
    
    }
    

    EDIT :

    To support numbers greater than 999.999, you can do like this :

    function fl_TapHandler(event:MouseEvent):void
    {
        score = score + 1;
        score_str = score.toString();   
        Taps_txt.text = add_commas(score_str);  
    }
    
    function add_commas(nb_str:String):String {
    
        var tmp_str:String = '';    
        nb_str = nb_str.split('').reverse().join('');
    
        for(var i = 0; i < nb_str.length; i++){
            if(i > 0 && i % 3 == 0) tmp_str += ',';
            tmp_str += nb_str.charAt(i);
        }
    
        return tmp_str.split('').reverse().join('');
    
        /*
    
            gives : 
    
                1234        => 1,234
                12345       => 12,345
                123456      => 123,456
                1234567     => 1,234,567
                12345678    => 12,345,678
                123456789   => 123,456,789
                1234567890  => 1,234,567,890
    
        */
    }
    

    Hope that can help you.