8 Replies Latest reply on Sep 6, 2006 5:42 PM by Rothrock

    Dinamic creation of text fields

    thunder.scripts
      Hi everybody. I created a bidimensional array in ActionScript with the necessary data to create text fields.

      With that information, I need to create as many textfields as elements the array has. I've attached the code I currently use.

      The problem is I don't know how to dinamically create the names for the fields I create. If all of them are called "text" then of course it won't work properly. The same thing happens with the format.

      One last thing, both x & y values are in pixels. How do I convert them so that they will work?

      Any ideas?

      Thanks a lot
      Fede
        • 1. Dinamic creation of text fields
          Craig Grummitt Level 3
          first, some comments:

          the font parameter needs to be a string.

          why have you called the array 'array' and then you refer to it as 'e1'?

          i don't understand why you want to 'convert' x and y pixels? pixels are Numbers and that's the variable type that createTextField accepts.

          now to give your text fields dynamic names - why not add 'i' to the word "text"? (see attached code)
          and similarly with your textformat:
          • 2. Re: Dinamic creation of text fields
            thunder.scripts Level 1
            quote:

            the font parameter needs to be a string.
            why have you called the array 'array' and then you refer to it as 'e1'?


            Sorry, it was just a quick example. The real name is el and font is a string.

            quote:

            i don't understand why you want to 'convert' x and y pixels? pixels are Numbers and that's the variable type that createTextField accepts.


            They are numbers, but createtextfield uses points as a measuring unit.It works, but the positioning isn't right. I'm not completely sure of this anyway, I'll check it when i have some time.

            quote:

            now to give your text fields dynamic names - why not add 'i' to the word "text"? (see attached code)
            and similarly with your textformat:


            Thanks a lot! I didn't know I could do that, especially the this["string"+var] to reference an element. I'll try it later.

            Thanks a lot for all your help!
            • 3. Re: Dinamic creation of text fields
              thunder.scripts Level 1
              One detail I forgot, I need the text fields to allow user input.
              Any ideas?
              • 4. Re: Dinamic creation of text fields
                thunder.scripts Level 1
                It worked perfectly! I just had to use _root.getNextHighestDepth() so that every textfield would be created on a different level.
                • 5. Re: Dinamic creation of text fields
                  Craig Grummitt Level 3
                  good one. i missed that you were using the same depth of 1.

                  >>createtextfield uses points as a measuring unit.
                  are you sure? i think you'll find createTextField uses pixels and not points as a measuring unit.

                  to allow user input use:
                  TextField.type = "input";
                  • 6. Re: Dinamic creation of text fields
                    Rothrock Level 5
                    Hello, I'm going to butt in on this one as well. Personally I think getNextHighestDepth() is a disaster waiting to happen. When used with certain components and all it can cause things you didn't quite intent.

                    There are millions of depths for you to use. Just pick a range and use an increment to add to it. So for example:

                    with(createTextField("text"+i, 1000+i, el [1], el[2], 500, 200))

                    And I'm with Crraig Grummitt on this one. Text fields are created in pixels. So there may not be a direct correlation to field size and textFormat point size. Check out the TextFormat.getTextExtent() method. That will tell you the pixel sizes needed to make a TextField the correct size for a given amount of text using a specific format.

                    PS: If you are using Flash 8 the help file might say getTextExtent() has been depreciated, but that is a documentation error. It is just fine!
                    • 7. Re: Dinamic creation of text fields
                      thunder.scripts Level 1
                      Well, don't worry, I stoped using getNextHighestDepth because you told me so in another topic :P

                      And yes, I was under the impression it used point, I suppose because I was doing something wrong and the positioning was weird. But everything is ok now.

                      Thanks everyboydy!
                      • 8. Re: Dinamic creation of text fields
                        Rothrock Level 5
                        Cool. I've really got a vendetta against getNextHighestDepth() maybe it is a personality flaw, perhaps it will be downfall someday, but so be it!