13 Replies Latest reply: Aug 26, 2012 4:26 AM by Ned Murphy RSS

    Dynamic TextField Array Help

    TwistedPixel23

      I want to be able to trace what the user has entered into the textbox, how do i go about it..

       

       

       

      [AS]

      quantityMenu.XXXSinput.addEventListener(KeyboardEvent.KEY_UP,XXXSupdateTextfield) 

      function XXXSupdateTextfield(evt:KeyboardEvent):void{

      //----------------------------------------------        

      //----------------------------------------------

      var num_clips:int = 999;

      //----------------------------------------------

      var nextYPos:int = 10;

      //----------------------------------------------

      var i:int = 0;

      //----------------------------------------------

      var container:MovieClip = new MovieClip();

      //----------------------------------------------

      addChild(container); for (i=0; i<num_clips ; i++) {

      //---------------------------------------------- 

      var boxMC:box = new box();

      //----------------------------------------------

      boxMC.x = 100;  boxMC.y = nextYPos;

      //---------------------------------------------- 

      //----------------------------------------------

      container.addChild(boxMC);

      //----------------------------------------------

      nextYPos+=boxMC.height+1;

      //---------------------------------------------- 

      if (int(quantityMenu.XXXSinput.text) > 1){    

      num_clips = int(quantityMenu.XXXSinput.text);

       

      }

      } [/AS]

       

      Any help is greatly appreicated

        • 1. Re: Dynamic TextField Array Help
          Ned Murphy CommunityMVP

          What textbox?  What does this posting have to do with an array?

          • 2. Re: Dynamic TextField Array Help
            TwistedPixel23 Community Member

            Sorry i havent explained myself properly, i have a movieclip with a texbox in it the MC is called "box". Then i have created a loop to dynammically add them to the stage depending on the number num_clips is changed to. I want to know how to add the loop to an array, so i can identify each instance of "box" and trace the text that was input by the user.

            • 3. Re: Dynamic TextField Array Help
              Ned Murphy CommunityMVP

              Just declare the array outside the loop (shown as boxArray below) and push each box instance into the array after it is instantiated inside the loop....

               

              var boxMC:box = new box();

               

              boxArray.push(boxMC);

               

               

              Then later on you only need to use the array to target each box's textfield text....

               

              for(var i:uint=0; i<boxArray.length, i++){

                    trace(boxArray[i].textFieldName.text);

              }

              • 4. Re: Dynamic TextField Array Help
                TwistedPixel23 Community Member

                Thanks Ned that worked a charm. Just one small thing that doesnt matter im just curious about is, when i get the trace result it skips loads of lines in the output the shows the result. Thanks very much again for you help

                • 5. Re: Dynamic TextField Array Help
                  TwistedPixel23 Community Member

                  Hi Ned, sorry i have reopend this but im still having trouble, this is the changed code,

                   

                  [AS]

                  var boxArray:Array = new Array

                   

                   

                  quantityMenu.XXXSinput.addEventListener(KeyboardEvent.KEY_UP,XXXSupdateTextfield)

                   

                   

                  function XXXSupdateTextfield(evt:KeyboardEvent):void{

                  //----------------------------------------------

                   

                   

                  //----------------------------------------------

                  var num_clips:int = 999;

                  //----------------------------------------------

                  var nextYPos:int = 10;

                  //----------------------------------------------

                  var i:int = 0;

                  //----------------------------------------------

                  var container:MovieClip = new MovieClip();

                   

                   

                  //----------------------------------------------

                  addChild(container);

                  for (i=0; i<num_clips ; i++) {

                  //----------------------------------------------

                  var boxMC:box = new box();

                  boxArray.push(boxMC);

                  //----------------------------------------------

                  boxMC.x = 85;

                  boxMC.y = nextYPos;

                   

                   

                  boxMC.SizeIndicator.text = ("XXXS")

                  //----------------------------------------------

                   

                   

                   

                  //----------------------------------------------

                  container.addChild(boxMC);

                  //----------------------------------------------

                  nextYPos+=boxMC.height+1;

                  //----------------------------------------------

                  if (int(quantityMenu.XXXSinput.text) > 1){

                      num_clips = int(quantityMenu.XXXSinput.text);

                   

                   

                   

                  }

                  quantityMenu.updateForm.addEventListener(MouseEvent.MOUSE_UP, traceText)

                            function traceText(e:MouseEvent):void{

                   

                   

                   

                                      for(var v:uint=0; v<boxArray.length; v++){

                          trace(boxArray[v].txTitle.text);

                  }

                  }

                  }

                   

                  [/AS]

                   

                  [AS]

                   

                  Which will trace what i put into the textfields, but when i try something like

                   

                  for(var v:uint=0; v<boxArray.length; v++){

                          trace(boxArray[v].txTitle.text);

                          XXXStext.text = (boxArray[v].txTitle.text);

                  }

                   

                  [/AS]

                   

                  Only the last thing entered into the textfield appears in XXXStext.text

                   

                  I have tried numerous things but cant come up with the soloution.

                  • 6. Re: Dynamic TextField Array Help
                    TwistedPixel23 Community Member

                    Can anyone help please?

                    • 7. Re: Dynamic TextField Array Help
                      Ned Murphy CommunityMVP

                      You should start a new posting if you have a new problem, especially after so much time has passed.  Tagging onto an old one makes it less noticeable.

                       

                      If you are saying the following code

                       

                         for(var v:uint=0; v<boxArray.length; v++){

                              trace(boxArray[v].txTitle.text);

                              XXXStext.text = (boxArray[v].txTitle.text);

                         }

                       

                      only shows that last item of whatever boxArray is, that is what you are telling it to do by assigning  ( = ) the value to it.  Each time it loops, you are reassigning it a new value by using the =.   If you use the appendText() method, it will build each one after another...

                       

                         for(var v:uint=0; v<boxArray.length; v++){

                              trace(boxArray[v].txTitle.text);

                              XXXStext.appendText(boxArray[v].txTitle.text);

                         }

                      • 8. Re: Dynamic TextField Array Help
                        TwistedPixel23 Community Member

                        Thanks Ned it worked, but it traces it multiple times. For example when the loop creates 2 textfields and i enter A in the first one and B in the second, it will trace A. AB, ABA, ABAB.

                         

                        Thanks again.

                        • 9. Re: Dynamic TextField Array Help
                          Ned Murphy CommunityMVP

                          You're welcome

                          • 10. Re: Dynamic TextField Array Help
                            TwistedPixel23 Community Member

                            Is there any way to make it just trace each textfield once?

                            • 11. Re: Dynamic TextField Array Help
                              Ned Murphy CommunityMVP

                              The way you show your code with dashed lines separating lines of code and no indentation to show structure makes it very difficult to read.  It looks like you have a function defined within a function.  That is something you should never do.

                               

                              I took your code and recreated what I can make of it, and it only traces A and B when I specify 2 textfields and enter A in  one and B in the other.

                               

                              var boxArray:Array = new Array();

                              quantityMenu.XXXSinput.addEventListener(KeyboardEvent.KEY_UP,XXXSupdateTextfield);


                              function XXXSupdateTextfield(evt:KeyboardEvent):void{

                                 var num_clips:int = 999;
                                 var nextYPos:int = 10;
                                 var i:int = 0;
                                 var container:MovieClip = new MovieClip();
                                 addChild(container);

                                 for (i=0; i<num_clips ; i++) {
                                    var boxMC:box = new box();
                                    boxArray.push(boxMC);
                                    boxMC.x = 85;
                                    boxMC.y = nextYPos;
                                    boxMC.SizeIndicator.text = ("XXXS")
                                    container.addChild(boxMC);
                                    nextYPos+=boxMC.height+1;
                               
                                    if (int(quantityMenu.XXXSinput.text) > 1){
                                      num_clips = int(quantityMenu.XXXSinput.text);
                                    }
                                 }

                               

                                 quantityMenu.updateForm.addEventListener(MouseEvent.MOUSE_UP, traceText);
                              }

                               

                              function traceText(e:MouseEvent):void{
                                  for(var v:uint=0; v<boxArray.length; v++){
                                      trace(boxArray[v].txTitle.text);
                                      XXXStext.text = (boxArray[v].txTitle.text);
                                  }
                              }

                              • 12. Re: Dynamic TextField Array Help
                                TwistedPixel23 Community Member

                                Thank you very much ned, once again you have saved me hours of stress. Is a function inside a function always bad or is it ok if one relys on the other?

                                • 13. Re: Dynamic TextField Array Help
                                  Ned Murphy CommunityMVP

                                  Repeating... That is something you should never do.  I can't really think of any situation where a function relies on another such that is needs to be contained within it.  As you can hopefully see in your case, there was no need for it at all -  the whole function was pulled out intact without any need to modify it.  In other cases where you might think it needs to be, there is always a way around it, such as declaring a variable outside a function to make it available to multiple functions.