2 Replies Latest reply on May 11, 2006 2:32 PM by BCA_9

    Array of movieclips

    BCA_9
      I have an array with the instance names of 150+ movieclips that i would like to set the ._visible property to true/false. How can i do this??? i thought this would work. Am I way off???

      for (var i:Number = 0; i < aAllCircles.length; i++){
      var myMC:MovieClip;
      myMC = aAllCircles i ; i IS IN SQUARE BRACKETS OF COURSE
      myMC._visible=false;
      }

      thanks in advance.
        • 1. Re: Array of movieclips
          Level 7
          BCA_9,

          > I have an array with the instance names of 150+ movieclips
          > that i would like to set the ._visible property to true/false.
          > How can i do this??? i thought this would work. Am I way
          > off???

          You're way off.

          > for (var i:Number = 0; i < aAllCircles.length; i++){

          This is your for() statement. So far, so good. aAllCircles is
          presumably your array. The Array class specifies an Array.length property,
          so that's fine.

          > var myMC:MovieClip;

          Here, you're declaring an undefined MovieClip reference. Still fine --
          though I doubt you'd really want to declare this variable anew all 150+
          times, right? You could declare this outside the loop.

          > myMC = aAllCircles;

          Now you're trying to set that variable, myMC, to the Array instance.
          But myMC is supposed to be a MovieClip! If aAllCircles contains references
          to movie clips (not just strings, which happen to reflect the intance names
          of your movie clips, then you should use your counter variable, i, to access
          the relevant index of the array.

          The web version of these forums usually has trouble with references to
          brackets and i (and other letters that happen to be HTML elements) ... maybe
          that part of your code got truncated, here. If so, I may not be seeing what
          you actually type. In any case, I'll use n as a counter and show you what
          should actually be happening here.

          var myMC:MovieClip;
          for (var n:Number = 0; n < aAllCircles.length; n++) {
          myMC = aAllCircles[n];
          myMC._visible = false;
          }

          Let me stress again, this only works if the elements in your array are
          actual movie clip references. I have a hunch that what your array actuall
          holds is a bunch of strings. If so, you'll need to use the eval() function
          or array access operator to translate those strings into actualy references.

          First, let's reduce the above lines into an even more compact form. No
          need for the myMC variable at all, as it turns out.

          for (var n:Number = 0; n < aAllCircles.length; n++) {
          aAllCircles[n]._visible = false;
          }

          Now, if your array actuall has strings, you need to do this ...

          for (var n:Number = 0; n < aAllCircles.length; n++) {
          this[aAllCircles[n]]._visible = false;
          }

          ... because the result of any number in that aAllCircles array -- e.g.
          aAllCircles[0] -- will return a string (if the array contains strings). I
          don't mean to pound this into the ground, but it happens quite a bit in this
          forum. The this[string here] business converts the string into a property
          reference of its parent. The global property "this" refers to whatever
          object or timeline it's in, so in this case, it refers to the timeline these
          movie clip presumably belong to.


          David
          stiller (at) quip (dot) net
          Dev essays: http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: Array of movieclips
            BCA_9 Level 1
            David,

            Thanks for your help and insight. I was using strings and using this with the array did the trick.

            Brian