1 Reply Latest reply on Nov 18, 2009 6:02 PM by Rothrock

    NEED HELP: Array's and MovieClips

    Angela Swift

      I received help on this site on this info-graphic im doing and the solution was to use an "array" which im not familiar with at all. the person that helped me added the following code that worked:

      var city_arr:Array = new Array("louisville");//filll that array with all nedded cities

      //make all the boxes invisible at start
      for (i=0;i<city_arr.length;i++){
          var citybox:String  = city_arr[i] + "_box";
          //you can make similar code for your buttons an dragfunctions
          this[citybox]._visible = false;
          this[citybox].drag_mc.onMouseDown = function(){
              this._parent.startDrag();
          }
          this[citybox].drag_mc.onMouseUp = function(){
              this._parent.stopDrag();
          }
          this[citybox].close_btn.onRelease = function(){
              this._parent._visible = false;
          }
      }

      heres the problem...since i dont know how to use arrays im stuck. i need to add at least 30 more cities to my info-graphic but don't know how to use the naming conventions of an array. does anyone know how to fix this? please help and see my file to get a better idea of what im trying to do. Only one of the houses works (when you click on it a pop-up appears, is draggable and has an x to close the pop-up)

        • 1. Re: NEED HELP: Array's and MovieClips
          Rothrock Level 5

          Nothing particularily difficult about arrays. They are sort of like one of those old post office sorting cubby things. When you have a bunch of things to store you can use an array (a bunch of little boxes) to store the items. Then when you need them back you say, "Give me the thing in box 5."

           

          The only tricky thing is that the first box is box zero, and the second box is box one...

           

          So you can add things to an array when you create it like this:

           

          var cities:Array=new Array("Portland","New York","Miami")

           

          Or you can create it and then "push" those items into those little boxes!

           

          var cities:Array=new Array();

           

          cities.push("Portland");

          cities.push("Miami");

          etc.

           

          In that case it just pushes each new city into the next available slot at the end of the array.

           

          And then you get them back by this:

           

          trace(cities[0]) \\ That will show Portland

           

          Notice the square brackets? Those say, "Look inside the array for the element I asked for."

           

          Now in your case you aren't just getting the names of cities, you want to convert the name into a movieclip. I wouldn't do it quite the way that is shown above. So I'm going to do it my way!

           

          var curCity:MovieClip=this[cities[i]+"_box"];

          curCity._visible=false;

          curCity.drag_mc.onMouseDown=dragCity;

          etc...

           

          So notice in the curCity assignment I've got two sets of square brackets. The ones with cities[] are just using that index variable to get the value of each element in cities — which is the string names you put in the array — and then the plus adds on the "_box" part. But all that is inside the square brackets with this[]. And that tells flash to look through the object "this" which is the timeline and find the item with the name inside it.

           

          Hope that helps?