8 Replies Latest reply on Dec 8, 2012 5:59 AM by Germaris

    Is it possible to somewhere store an array?

    Germaris Level 1

      Hi there!

       

      I use arrays which I wish to re-use in some other frames of my SWF.

      Is it possible to store them somewhere in a secure way instead of asking the DB each time I want to use an array already downloaded?

       

      Shared Object maybe?

       

      Many thanks in advance for any tip!

        • 1. Re: Is it possible to somewhere store an array?
          kglad Adobe Community Professional & MVP

          any object (like an array) created with actionscript will be available everywhere in the swf where it was created.  you just need the correct path/variable reference.

           

          the only reason to use a sharedobject would be if you wanted the array available outside the swf where it was created.

          • 2. Re: Is it possible to somewhere store an array?
            Germaris Level 1

            Thank you, kglad, for replying.

             

            OK, I understand the principle. But...

             

            Would you please be more specific about: "you just need the correct path/variable reference"? (by providing a sample)

             

            Variable (for example) "myArray" is used several times in my SWT for different arrays...

            Should I rename all of them with different names?

             

            And I don't know how to define a "path" inside the SWF...

            Is "path", the frame where the array has been created?

             

            Regards,

             

            Gerard

            • 3. Re: Is it possible to somewhere store an array?
              kglad Adobe Community Professional & MVP

              path, for code attached to frames, refers to the timeline/movieclip that contains the frame.

               

              any array whose values you need in the future should NOT have its values overwritten.  ie, if you initially define myArray you should not redefine it if you subsequently want to retrieve those initial values.

              • 4. Re: Is it possible to somewhere store an array?
                Germaris Level 1

                OK.

                 

                Here is the code I use:

                 

                In the first frame of a dedicated layer, I wrote the following function to ask the DB for the data the array will contain. Result is obviously available from any frame of the movie.

                 

                userRemindersFct = function () {

                    var myUserReminders = new LoadVars();

                    myUserReminders.action = "rappels";

                    myUserReminders.unlocking = "1";

                    myUserReminders.onLoad = function(ok) {

                        if (ok) {

                            var myUserRemindersArray:Array = new Array();

                            myUserRemindersArray = myUserReminders.mydata;

                            myUserRemindersArray = myUserRemindersArray.split("|");

                        }

                        else {

                            stop();

                        }

                    };

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

                };

                userRemindersFct();

                 

                In another frame, and only in this frame, I use the following code, specific and limited to this frame to display sequentially the elements of the array

                 

                _root.myUserRemindersArray = Array();

                //calls for the array obtained with the above function

                var counter = 0;

                displayUserArrayFct = function () {

                    _root.reminderFld.html = true;

                    _root.reminderFld.htmlText = _root.myUserRemindersArray[counter];

                    var timeDelay = _root.reminderFld.length * 100;

                    // one tenth of a second per character

                    counter++;

                    var sto = setTimeout(displayUserArrayFct, timeDelay);

                    if (counter == myUserRemindersArray.length) {

                        counter = 0;

                    }

                };

                displayUserArrayFct();

                 

                In reminderFld, I get "undefined"!!!

                 

                What's wrong?

                • 5. Re: Is it possible to somewhere store an array?
                  kglad Adobe Community Professional & MVP

                  you made myUserRemindersArray local to your onLoad function by declaring the array in that function.  ie, you declared that array to have scope  myUserReminders.onLoad .

                   

                  to remedy, use:

                   


                  var myUserRemindersArray:Array;  // this array can be referenced anywhere on this timeline after this line of code executes but it will be null at this point.

                  userRemindersFct = function () {

                      var myUserReminders = new LoadVars();

                      myUserReminders.action = "rappels";

                      myUserReminders.unlocking = "1";

                      myUserReminders.onLoad = function(ok) {

                          if (ok) {

                             myUserRemindersArray = new Array();  // at this point the array is instantiated but has no contents

                              myUserRemindersArray = myUserReminders.mydata;

                              myUserRemindersArray = myUserRemindersArray.split("|");  // at this point the array has the contents you expect

                          }

                          else {

                              stop();

                          }

                      };

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

                  };

                  userRemindersFct();

                   

                  // at this point the array is still null because your onLoad executes AFTER this line of code.

                   

                  p.s. please mark helpful/correct responses.

                  • 6. Re: Is it possible to somewhere store an array?
                    Germaris Level 1

                    Oh!

                    Thank you very much. It works.

                     

                    Somebody told me long time ago: "Always declare your variables first!"

                    It was you.

                     

                    Best regards,

                     

                    Gerard

                    • 7. Re: Is it possible to somewhere store an array?
                      kglad Adobe Community Professional & MVP

                      you're welcome.

                       

                      and, by convention, import statements are the first lines of code followed by variable declarations followed by the rest of your code.

                      • 8. Re: Is it possible to somewhere store an array?
                        Germaris Level 1

                        I won't forget it.

                        Promised!