20 Replies Latest reply on Sep 17, 2007 3:22 AM by Greg Dove

    creating a list of textfields.

    GregConnTelevirtual
      How can i create a list of textfields dynamically through actionscript.

      I have textfields and they all effect the one before them, so a list would be useful. I need to be able to delete the first one in the list and them to reposition themselves automatically this is why i cant use an array.

      any ideas how i can do this?
        • 1. Re: creating a list of textfields.
          Greg Dove Level 4
          You can use an array... and a loop

          when you delete the first one, remove it from the array using the shift method or splice(0,1) and then loop through the array repositioning each textfield element.
          • 2. Re: creating a list of textfields.
            GregConnTelevirtual Level 1
            That sounds like a plan thank you GWD. I'm trying this.

            I've made an array of textfields but for some reason it has now stopped displaying on the screen even though the content is all there....? Do i need to do something else as i've changed it an array?
            • 3. Re: creating a list of textfields.
              Greg Dove Level 4
              Its not possible to answer your question with the information available.

              When you create the textfields push references to them onto the array with array push. If you're positioning on screen from left to right with each new textfield, then the array has them in the correct order.
              If you want to removeTextField for the left most textfield, then just use array.shift to remove the first element which is your left most textfield and removeTextField it.
              Then loop through your array resetting the _x positions.

              That's one example.
              Another would be that if the textfields are all identical width then you could just loop through the array assigning the .text property from the next textfield to the current one in the loop. And remove the last one.

              If you're stuck on something post the code that's causing problems.
              • 4. creating a list of textfields.
                GregConnTelevirtual Level 1
                Sorry i was very vague,

                the first input goes into a variable the first textfield of fixed width is then filled with this variable. I check the number of characters in the variable and by knowing the width and the amount of characters that can fit on one line, this then helps me work out what the height of the textfield is. I then position this in a fixed x position and the y position becomes the size of the screen - the height so it's at the bottom of the viewable area and it all fits in. The next item that comes in does the same but moves the previous item up buy the new items height and so on and so forth. I works when the textfields are just individual and doesnt work when they are an array of textfields, display just doesnt work.

                I cant think of how to explain the code any better than just showing you it so apologies for the dump. The code is housed in a class so is called and run from the main fla file.


                Cheers for getting to the bottom and i really do apologise for all the code. hope you can help

                Greg
                • 5. Re: creating a list of textfields.
                  Greg Dove Level 4
                  Would you mind editing that message and reposting the code in an attach Code popup. As it is its not showing correctly because some of it is being interpreted as formatting code.

                  Hmmm. That's not quite what I meant - there are problems with that. But it will be easier to work on if you can reformat in the attach Code box.

                  example:
                  _root.createTextField(_root.textbox[j], 2,608,82,275,650);

                  doesn't make sense unless _root.textbox[j] is a string that you want to call the textfield.

                  I meant something more like
                  var newTf= _root.createTextField("tf"+j, 2,608,82,275,650);
                  _root.textBoxArray.push(newTf).
                  newTf.property = value;
                  newTf.property=value;

                  etc.

                  • 6. Re: creating a list of textfields.
                    GregConnTelevirtual Level 1
                    code is now attached.

                    i see what you mean, so if i make your

                    var newTf
                    into
                    var newTf = new Array();

                    newTf[j] = _root.createTextField("tf"+j, 2,608,82,275,650);

                    would that be right?

                    thanks

                    Greg
                    • 7. Re: creating a list of textfields.
                      Greg Dove Level 4
                      I think there's something missing there. You missing your [ i ] 's (I don't know how many there were). Perhaps you copied and pasted the post above instead of your original code.
                      Please note you can choose edit on your previous post rather than reposting code. When you choose edit you get to edit your regular text. If you choose attach Code from there, you're editing the code associated with that post... so you don't need to repost another chunk of code, just re-edit above.

                      In answer to your last question, no, that's not what I meant either. I just mean that for each instance of your textfields, you create them, manipulate them - do this all with a local variable in your function... no need for an index. And then push them onto your array (which starts as empty)

                      Im not sure where j (and possibly i) are coming from.

                      Is _root.textbox set up as an empty array.?
                      • 8. Re: creating a list of textfields.
                        GregConnTelevirtual Level 1
                        i've editted the original post, i's are back but they seem to be capitals.

                        i and j are declared in the class. sChat is an array of Strings.
                        _root.textbox is set up in the fla (main class) as textbox = [[ ]] so yes it's an empty array.

                        thanks

                        Greg
                        • 9. Re: creating a list of textfields.
                          Greg Dove Level 4
                          OK...you could get the j value just from array.length if you were pushing onto arrays each time which was the reason for my last comment. Rather than changing your code, I'm going to go for a real quick guess
                          try changing these
                          _root.createTextField(_root.textbox[j], 2,608,82,275,650);
                          to be more like this.
                          _root.textbox[j] = _root.createTextField("userChat"+j, 2,608,82,275,650);

                          see if that makes a difference
                          • 10. Re: creating a list of textfields.
                            Greg Dove Level 4
                            And that 2 in the createTextField above should probably be
                            _root.getNextHighestDepth()

                            instead
                            • 11. Re: creating a list of textfields.
                              GregConnTelevirtual Level 1
                              Just changing that item doesnt make a difference.

                              • 12. Re: creating a list of textfields.
                                GregConnTelevirtual Level 1
                                ok it make a small difference i'm now continually printing the user inpout on top of user input and aiInput ontop of ai input, this is fine as i can obviously change this with a loop.....

                                What relevance does having _root.textbox[j] = in the code, i cant see what it will do, what does it store? does it store all the properties of the textfield it creates?

                                thanks for all of this by the way!
                                • 13. Re: creating a list of textfields.
                                  GregConnTelevirtual Level 1
                                  one other thing, it only works if i change all the other _root.textbox[j]'s to what is in the createTextfield parenthesis, so in this case _root["userChat"+j] because i needed to edit the properties of the textfield.
                                  • 14. Re: creating a list of textfields.
                                    Greg Dove Level 4
                                    I meant that as an example - so for both. actually I should have called that one "OtherChat"+i - the first one is "UserChat"+i for example. So the other one needs to change as well.

                                    And I'm assuming the first time around that
                                    _root.textBox is defined and is empty so its [] and not [[]] as you posted although it won't matter if j is zero.
                                    j is defined and accessible within the scope of that code. And it should - if I understand correctly - be zero the first time its run.

                                    BTW a textfield has no height property in as2. Its _height

                                    • 15. Re: creating a list of textfields.
                                      Greg Dove Level 4
                                      Sorry I didn't catch your last two posts
                                      • 16. Re: creating a list of textfields.
                                        GregConnTelevirtual Level 1
                                        yeah j is zero first time round. I'll try those other bits.

                                        i have to leave now, can i will attempt these on monday, thanks so much for your help. i think it's close!

                                        thanks again

                                        Greg
                                        • 17. Re: creating a list of textfields.
                                          Greg Dove Level 4
                                          Are you absolutely sure that

                                          _root.textbox is an array?
                                          (It doesn't sound like the name of an array)
                                          That would be causing the problem you describe above.

                                          If it is an array then it just places a reference to the new textfield at the j index in that array.
                                          So you can access the textfield and set its properties from the array - by accessing the item at any index. Its not a copy of the textfield... its just a pointer or refernce to it.
                                          • 19. Re: creating a list of textfields.
                                            GregConnTelevirtual Level 1
                                            Hi GWD,

                                            I checked this morning and i had made a stupid error (it was end of day Friday evening i think my brain had switched off), it's now working with _root.textbox[] working as a reference to the textfield in position j. I'm now going to make it all work visually and then i gotta get onto deleting the the texfields that have moved off of the screen and then resorting the array.

                                            thanks for your help!

                                            Greg
                                            • 20. Re: creating a list of textfields.
                                              Greg Dove Level 4
                                              great to hear. Good luck with it
                                              cheers
                                              GWD