A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: AS3 Making Digits start from 0000?

  1. #1
    Junior Member
    Join Date
    Jul 2007
    Posts
    9

    AS3 Making Digits start from 0000?

    Hi All

    Man, what a difference AS3 is to AS2, but good fun!

    I have the following code on a movie timeline :

    Actionscript Code:
    var myvalue:int;
    myvalue = 1;
    mytextbox_inst.text = "Todays Value is : " + myvalue.toString();

    I want the dynamic text to show "Todays Value is : 0001" but its showing "Todays Value is : 1".
    I just cannot remember how to make the dynamic text start as 4 digits?
    Can you please help?

    Many thanks
    AlbertSteptoe

  2. #2
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Actionscript Code:
    var unitLength:String="0000";
    var formatVal:String;

    function set formatedValue(val:*):void {
        var diff:int=unitLength.length-val.toString().length;
        formatVal=unitLength.substr(0,diff)+val;
    }
    function get formatedValue():String {
        return formatVal;
    }



    var myVal:int;
    myVal=1;
    formatedValue=myVal;
    trace(formatedValue);

    mytextbox_inst.text= "Todays Value is : " + formatedValue;




    arkitx
    Last edited by arkitx; 10-23-2011 at 09:01 AM.

  3. #3
    Junior Member
    Join Date
    Jul 2007
    Posts
    9
    Hi arkitx

    Thanks for the reply.

    Do i REALLY need to add all that just to get it to show 0001?
    Previously in AS2 i'm sure you just added something to the dynamic line and was hoping AS3 had a little fix as well.

    Will give your suggestion a go but hoping a smaller amount of code would do the job.

    Thanks for your help.
    Cheers!
    AlbertSteptoe

  4. #4
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    AS3 is more OOP language. So by doing this you can have more flexibility throughout your application. If you want to work with Classes this will help you a lot. You can also apply this code structure in AS2 by replacing the int with Number

    Hope you will understand more when you enter deep into Class Base Application.

    You can also do it in one line of code in AS3.

    Actionscript Code:
    var unitLength:String="0000";
    function formatedText(val:int,uLen:String) {   
        return uLen.substr(0,uLen.length - val.toString().length) + val;
    }

    var myVal:int;
    myVal=1;
    mytextbox_inst1.text="Todays Value is : " +formatedText(myVal,unitLength);
    trace(formatedText(myVal,unitLength));




    arkitx
    Last edited by arkitx; 10-23-2011 at 09:51 AM.

  5. #5
    Junior Member
    Join Date
    Jul 2007
    Posts
    9
    Thanks You, that is REALLY helpful

    The coding change between AS2 & AS3 is amazing!
    Will give that method a try, i'm sure it will solve my problem and will reply with the results soon.

    Thanks again!
    albertsteptoe

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center