14 Replies Latest reply on Dec 6, 2012 1:19 PM by Germaris

    AS 2: Sequentially display all of an array items, one after another

    Germaris Level 1

      Hi there!

       

      This is a start afresh of another thread (http://forums.adobe.com/message/4897959#4897959).

      (ActionScript 2 ONLY)

       

      My site is a SWF which loads on user requests data from a MySQL DB via PHP.

      I have no problem mastering the whole communication process.

       

      On the Home page, along with News which are already presented in the main right part of the page, I wish to display on the left part some reminders of essential services offered and also mandatory actions registered users must do when visiting the site.

       

      The DB returns: an array with a | delimiter and the total of the array items.

       

      In Flash, I use the following function to get the array:

       

      remindersFct = function () {

                var myReminders = new LoadVars();

                myReminders.identity = "reminders";

                myReminders.onLoad = function(ok) {

                          if (ok) {

                                         _root.reminderFld.html = true;

                                         var myArray:Array = new Array(myReminders.mydata.split("|"));

                                         myArray.length = myReminders.totalItems;

                          }

                          else {

                                         stop();

                          }

                };

                myReminders.sendAndLoad("misc.php",myReminders,'POST');

      };

      remindersFct();//start the process

       

      I want to know differents things...:

       

      1 - How can I "explode" this array into its differents items (all of them are strings of text)

      2 - Then, how can I tell the SWF to display them, one after another, in the appropriate htmlText field

      3 - How can I adjust the duration of each of these items based on the length of the text they contain

       

      Many thanks in advance for your help!

        • 1. Re: AS 2: Sequentially display all of an array items, one after another
          Ned Murphy Adobe Community Professional & MVP

          1) What do you mean when you say you want to explode the array into its different items?

           

          2) This was already covered in the other thread, you just need to fix how you did it. 

           

          3)  You need to decide how you want to make use of the number of characters to set the display/delay time.  If the string has some number of characters, how long would you expect it to be displayed - just take any string and make that decision.  If you can decide on that, then you have a seconds per character basis that you can utilize to calculate the delay you need to assign for any string.

          • 2. Re: AS 2: Sequentially display all of an array items, one after another
            Germaris Level 1

            Thanks for your patience, Ned!

             

            1- I just want to know how to do this because if I fail for the other steps, I could use another solution, less elegant but efficient (I think...). For example, creating a movieclip with a text field in which each element will be inserted. This MC having a countdown to flush the text and load the next text.

             

            2- I wasn't able to fix it. I only got worse situation getting a warning message from the flash player of the browser telling me that a script became unresponsive and encouraging me to abort it if I don't want to crash my browser.

             

            3- That's okay for understanding. Practice is something different.

             

            My coding abilities in Flash are somewhat limited and my age (72) isn't really an asset!

             

            :-)

             

            In the other thread, you gave me this code:

             

            function displayText(){

                      tField,text = textArray[counter];

                      var timeDelay:Number =  someFunction(tField.length);

                      counter += 1;

                      if(counter < textArray.length){   

                            var sto = setTimeout(displayText, timeDelay);

                      }

            }

             

            Could you explain how is it possible to have a function running into itself?

            (see line at var sto = setTimeout(displayText, timeDelay); )

            • 3. Re: AS 2: Sequentially display all of an array items, one after another
              Ned Murphy Adobe Community Professional & MVP

              1) I don't know what "this" is, that's is why I am asking what you meant with what you said.

               

              2)  Below is some code as an example.  Open a new file and place this code in the timeline.  Then add a textfield and give it an instance name of tField.  If you run the file it should show the text cycling thru

               

              3) ?  I don't understand, but if it helps, it is logical thought that will solve this, not practice.  The example below shows one simple way, it just assigns some number of milliseconds for each charcater in the string...

               

              var theText:Array = new Array("One", "Two", "Three");
              var counter = 0;

               

              function displayText(){
                  tField.text = theText[counter];
                  var timeDelay = tField.length * 500; // one half second per character
                  counter++;
                  if(counter < theText.length){
                     var sto = setTimeout(displayText, timeDelay);
                  }
              }

               

              displayText();

               

               

              The function isn't really running into itself.  Another function (setTimeout) within the function is re-executing the function. 

               

              Coding abilities are age-less... you are not alone.

              1 person found this helpful
              • 4. Re: AS 2: Sequentially display all of an array items, one after another
                Germaris Level 1

                Thanks Ned, you're very kind person...

                 

                1- "this" is to get the content of the array, like slices of a sausage, and use these slices as text strings I can then manipulate as I need.

                 

                2- I made the file and of course, -it's not big news for you-, it works.

                Just a little observation:

                In the first line of the code, the content of the array is known an written down (new Array("One", "Two", "Three")).

                What if I replace this line (for use in my real world file) with:

                var theText:Array = new Array(myReminders.mydata.split("|"));? (see the code in my first posting above)

                I think I must write some code to get the content theText to appear string after string...

                 

                Just another thing... (no, no, ain't a ghost!!!)

                 

                When I tested your sample movie, the display stops when string "Three" is displayed.

                How do I make the display starting over "One", "Two", "Three" and again and again and...(a loop maybe)?

                • 5. Re: AS 2: Sequentially display all of an array items, one after another
                  Ned Murphy Adobe Community Professional & MVP

                  Once you have the data stored in the array the way you do, you have your slices ready.  My array would be the same as your array, yours is just filled dynamically.  As soon as you have the data stored in the array, you can call the displayText() function on the very next line to get the textr displaying in the textfield.

                   

                  In your original posting above you show a line:  myArray.length = myReminders.totalItems;

                   

                  the "length" is a prperty of the array and you cannot assign it.  As you can see in the code I offered it does get used, but it did not need to be assigned a value... its value is defined by how many elements the array contains.

                   

                  As far as getting the text to cycle goes, you just need to reset the counter variable back to 0 when it reaches the end....

                   

                     if(counter < theText.length){

                         var sto = setTimeout(displayText, timeDelay);

                      } else {

                          counter = 0;

                      }

                  • 6. Re: AS 2: Sequentially display all of an array items, one after another
                    Ned Murphy Adobe Community Professional & MVP

                    I might be wrong with the way I wrote the reset for the counter.  I think the way I wrote it, it will reset, but will also stop since it skips the setTimeout.  Here's what I think is correct...

                     

                        if(counter < theText.length){ 

                             var sto = setTimeout(displayText, timeDelay);

                             if(counter == theText.length){

                                  counter = 0;

                             }

                        }

                    • 7. Re: AS 2: Sequentially display all of an array items, one after another
                      Germaris Level 1

                      I owe you so much!!!

                      It works nice.

                       

                      The only thing which doesn't work is else statement...

                      Don't know why...

                      I tried it out in your sample .fla and it doesn't work either.

                       

                      Thank you very much anyway!

                      • 8. Re: AS 2: Sequentially display all of an array items, one after another
                        Germaris Level 1

                        Sorry, but the alternate version you give in your post #6 doesn't work...

                        I tried it out in your sample .fla...

                        • 9. Re: AS 2: Sequentially display all of an array items, one after another
                          Germaris Level 1

                          This is the working solution I found:

                           

                          var theText:Array = new Array("One", "Two", "Three");

                          var counter = 0;

                          function displayText() {

                                    tField.text = theText[counter];

                                    var timeDelay = tField.length * 500;// one half second per character

                                    counter++;

                                    if (counter < theText.length) {

                                              var sto = setTimeout(displayText, timeDelay);

                                    }

                                    if (counter == theText.length) {

                                              counter = 0;

                                              var sto = setTimeout(displayText, timeDelay);

                                    }

                          }

                          displayText();

                           

                          Who's the geek?

                           

                          :-)   :-)   :-)

                          • 10. Re: AS 2: Sequentially display all of an array items, one after another
                            Ned Murphy Adobe Community Professional & MVP

                            Yeah, my brain is at its limit for the day I guess.  In my second example it would never get into to second conditional.  See, age is not the culprit, just the brain.

                            • 11. Re: AS 2: Sequentially display all of an array items, one after another
                              Ned Murphy Adobe Community Professional & MVP

                              Just one other thing.  Since you call that setTimeout for both halves of the conditional, that means you don't need it in the conditional and can move it outside, and you don't need the first half of the conditional either...

                               

                                        counter++;

                                        var sto = setTimeout(displayText, timeDelay);

                                        if (counter == theText.length) {

                                                  counter = 0;

                                        }

                              • 12. Re: AS 2: Sequentially display all of an array items, one after another
                                Germaris Level 1

                                Even if you're a little tired, you still can use your brain efficiently...

                                I'll give a try tomorrow to ripping off the first half of the conditional.

                                 

                                I thank you for the help you provided and which drove me to the goal!

                                 

                                Best regards,

                                 

                                Gerard

                                • 14. Re: AS 2: Sequentially display all of an array items, one after another
                                  Germaris Level 1

                                  "Perfection", where are you?

                                  :-)

                                   

                                  I gave a try without the first half of the conditional and it works nice.

                                   

                                  In search of "Perfection", I need to know if it is possible to somewhere store the array?

                                   

                                  Little explanation:

                                  The display of the array takes place when the user first arrives on the Home Page.

                                  Then he/she navigates. But sometimes, returns to the Home Page.

                                  In order to simplify the process, it will be good not to ask again the DB and use the stored array.