3 Replies Latest reply on Jun 19, 2006 7:45 PM by NSurveyor


      i'm working on a project and i thought that an easy way to get around a problem i have would be to make an array to store info and then output it later. But i have no idea how to make an array in flash... could someone please explain how to define an array and possibly ouput the contents of it in a text box, with each 'cell' of the array on a new line in the text box.

        • 1. Re: arrays
          This is the kind of question that you should get the answer from reading the help docs that comes with Flash.... but anyways,

          To create array, use either, new Array() or the shorthand, [ ]. You can use new Array() to make an array with a specific length, or give the actual elements in it. E.g.:

          myArray = new Array(10);
          trace(myArray.length); // outputs 10
          trace(myArray); // 10x undefined => values not defined yet


          myArray = new Array("a ball","a cat",32324324);

          With [ ], it would be:

          myArray = ["a ball","a cat",32324324];

          You can manipulate individual values, using the array access operator. Arrays are indexed 0-based.

          myArray = ["a ball","a cat",32324324];
          trace(myArray[0]); // "a ball"
          myArray[1] = "a dog";
          trace(myArray); // a ball, a dog, 32324324

          You can add values to the array using push:

          myArray = ["a ball","a cat",32324324];
          trace(myArray); // a ball, a cat, 32324324, eeeeeeeeeeeek

          To put the contents of the array, myArray into a textfield, my_txt, use:

          my_txt.text = myArray.join('\n');

          But really, if you want it explained thoroughly, read the docs! :-)
          • 2. Re: arrays
            Level 7
            Note the trap here ...

            x = new Array("10")

            gives you an array with the string "10" in it

            x = new Array(10)

            gives you an array with 10 undefined values

            That's why its best to NOT use the new Array(x) notation .. as sometimes the
            x is the array content, and sometimes the number of elements.

            The [ ] notation will always give you an array with the required content.

            If you really need an array preset with 'n' undefined values, create an
            empty array and add the 'n' undefined values with a for-loop use [undefined,
            undefined, undefined, undefined] etc

            That way you don't need to stop and think when reading your code next time
            whether the arg for new Array() is a count or the new array content.

            • 3. Re: arrays
              NSurveyor Level 2
              Or without a for loop: