1 Reply Latest reply: Aug 7, 2012 4:26 AM by Ned Murphy RSS

    Compressing Text using Loops

    Skillatron

      Hi,

      I'm currently working on a basic random number generator type program where you enter several variables into input boxes and it produces numbers. One of the variables is the number of numbers generated (1-6). For this I have 6 output boxes (dynamic text boxes named Output1_txt, Output2_txt, up until Output6_txt) If there is 1 number generated then Output1_txt shows the string of the variable 'number1' (a random number that was generated earlier in the function) and the rest show the string ' ' (so just blank). If 2 numbers are generated Output1_txt shows number1, Output2_txt shows number2 and the rest are blank again. This continues up until Output6_txt.

      I'm not very experienced at flash so at the moment my code is very very very long winded (Output = the number of outputs inputted).

      So here is my code at the moment:

       

       

                if(Output == String(0)){

                          Output1_txt.text = ('')

                          Output2_txt.text = ('')

                          Output3_txt.text = ('')

                          Output4_txt.text = ('')

                          Output5_txt.text = ('')

                          Output6_txt.text = ('')

       

                }else if(Output == String(1)){

                          Output1_txt.text = number1;

                          Output2_txt.text = ('')

                          Output3_txt.text = ('')

                          Output4_txt.text = ('')

                          Output5_txt.text = ('')

                          Output6_txt.text = ('')

       

                }else if(Output == String(2)){

                          Output1_txt.text = number1;

                          Output2_txt.text = number2;

                          Output3_txt.text = ('')

                          Output4_txt.text = ('')

                          Output5_txt.text = ('')

                          Output6_txt.text = ('')

       

                }else if(Output == String(3)){

                          Output1_txt.text = number1;

                          Output2_txt.text = number2;

                          Output3_txt.text = number3;

                          Output4_txt.text = ('')

                          Output5_txt.text = ('')

                          Output6_txt.text = ('')

       

                }else if(Output == String(4)){

                          Output1_txt.text = number1;

                          Output2_txt.text = number2;

                          Output3_txt.text = number3;

                          Output4_txt.text = number4;

                          Output5_txt.text = ('')

                          Output6_txt.text = ('')

       

       

                }else if(Output == String(5)){

                          Output1_txt.text = number1;

                          Output2_txt.text = number2;

                          Output3_txt.text = number3

                          Output4_txt.text = number4

                          Output5_txt.text = number5

                          Output6_txt.text = ('')

       

                }else if(Output == String(6)){

                          Output1_txt.text = number1;

                          Output2_txt.text = number2;

                          Output3_txt.text = number3

                          Output4_txt.text = number4

                          Output5_txt.text = number5

                          Output6_txt.text = number6

       

       

      This works fine for my program but it seems ridiculous to have to manually type in all those lines (especially since I want to expand to 9 outputs). My question is how I can compress this to fewer lines (using loops probably?)

      Sorry if it is a bit confusing but I'm still getting used to Flash.

       

      Thank you so much

        • 1. Re: Compressing Text using Loops
          Ned Murphy MVP

          Without seeing what else is involved, the only suggestion I can make is that you only need to have the last group executed...

           

                              Output1_txt.text = number1;

                              Output2_txt.text = number2;

                              Output3_txt.text = number3

                              Output4_txt.text = number4

                              Output5_txt.text = number5

                              Output6_txt.text = number6

           

          You just need to control the values of the number variables (number1 thru number6)... they are either number values (as Strings) or they are empty Strings ""

           

          Each time your program iterates a set of the numbers it should first reset all the number#'s to "".

           

          As far as shortening that last set into a loop form, you could use bracket notation, which allows you to use Strings to target objects, as in...

           

                       for(var i:uint=1; i<7; i++){

                            this{"Output"+String(i)+"_txt"].text = this["number"+String(i)];

                       }