3 Replies Latest reply on Jan 10, 2010 1:04 PM by kglad

    Global Array with variable name

    Nicholas Avallone Level 1

      I am trying to create a global array with a variable name. I've got the variable name bit working;

       

          random(5) = q;
          this["array"+q] = new Array();

       

      But I can't figure out how to make the whole array global and accessible to the rest of my timeline. Anyone have ideas? Thanks in advance!

        • 1. Re: Global Array with variable name
          kglad Adobe Community Professional & MVP

          that code doesn't make sense.

           

          but, you would prefix the variable name with _global to make a variable global:

           

          _global.array1 = [1,2,3];

           

          //or

           

          _global["array"+1] = [1,2,3];

          1 person found this helpful
          • 2. Re: Global Array with variable name
            Nicholas Avallone Level 1

            Awesome. Yeah, it's a fragment of a larger script — a script which probably also doesn't make sense, but it works now, so thanks!

             

            _global.listItems = new Array("Apples","Bananas","Pears","Cherries","Grapes","Cabbages","Tomatoes");

             

            function newPop(popName):Function
                {
                var q = popName;
                _global["pop"+q] = new Array();
                var i;
                var j;
                for (i=0; i<listItems.length; i++){
                    if (i==0) {j=random(200)*5}
                    else if(random(100) <= 15){
                        j = random(200);
                    } else {j=0;}
                k = listItems[i];
                _global["pop"+q].push(k,j);
                }
                return _global["pop"+q];
                }
               
            trace(newPop("Farmtown"));

             

            I needed to set up a bunch of global arrays at the start of the timeline, and the values need to be random (weighted and always present for "Apples" in the example), so having this script working saves a lot of brute-force input.

             

            Thanks again!

            • 3. Re: Global Array with variable name
              kglad Adobe Community Professional & MVP

              you're welcome.