2 Replies Latest reply on Jul 15, 2010 3:32 PM by SOMEONEINTHEWORLD

    Label Text

    SOMEONEINTHEWORLD

      Hello,

       

      I have labels, label1, label2, label3.....label11.

       

      i want change their texts,

       

       

      The below code is not working,  can someone tell me how to change the text using loop?

      public function init():void
                  {
                      for(var i:Number=0; i<=10; i++)
                      {
                          var str:String = "label"+i;
                         
                          str.text = i;                                  
                      }
                  }

       

       

      Thanks

        • 1. Re: Label Text
          Solydest Level 2

          Hi,

           

          The reason it doesn't work is that str is just a variable of type String, it doesn't actually point to your label control(s) even though they happen to have the same id as the value of str.  One way to do it would be to put all of the label controls in an array and loop through the array:

           

          public var labels:Array = new Array();
          
          ...add all your labels to the array
          
          for(var i:int = 0; i < labels.length; i++)
          {
               labels[i].text = i;
          }
          

           

          I'm sure there are other ways to do it, but I believe this way most closely resembles what you are trying to do in your example.

           

          Cheers

          • 2. Re: Label Text
            SOMEONEINTHEWORLD Level 1

            Thanks.