5 Replies Latest reply on Nov 28, 2007 9:28 AM by Damon Edwards

    calling array in function

    4pixels
      on frame 1 of root:
      //declare and fill array "mcPopups" with movie clip instance names
      var mcPopups = [ "mcAlta1", "mcAlta2", "mcAlta3", "mcAlta4", "mcAlta5" ];

      //declare function which goes to hidden label in all movie clips from array "mcPopups"
      function turnOff() {
      _root.mcPopups.gotoAndStop("hidden");
      }

      then on the frame for a button in my movie clip mcAlta1 I have the code:
      stop();

      //call function "turnOff();"
      close_btn.onRelease = function() {
      _root.turnOff();
      };

      Am I doing something wrong here as it doesn't work ie the movie clips should all go to "hidden" when I click my "close_btn"

        • 1. Re: calling array in function
          Level 7
          >> //declare and fill array "mcPopups" with movie clip instance names
          var mcPopups = [ "mcAlta1", "mcAlta2", "mcAlta3", "mcAlta4", "mcAlta5" ];

          //declare function which goes to hidden label in all movie clips from array
          "mcPopups"
          function turnOff() {
          _root.mcPopups.gotoAndStop("hidden");
          }


          First, you are putting strings in the array, and you probably want your
          actual instances. So change the first part to:

          var mcPopups = [mcAlta1, mcAlta2, mcAlta3, mcAlta4, mcAlta5];

          Next, you need to actually iterate through the array, one item at a time.
          The array contains MC references - which you can tell to gotoAndStop - an
          array does not have a gotoAndStop method so array.gotoAndStop() makes no
          sense. Change your function to:

          function turnOff(){
          for(var i = 0; i < mcPopups.length; i++){
          mcPopups .gotoAndStop("hidden");
          }
          }

          Lastly, it's recommended to not use _root references as root can change.
          Better to do things like this._parent if you need to, but in your code just
          removing all _root should be fine.


          --
          Dave -
          Head Developer
          http://www.blurredistinction.com
          Adobe Community Expert
          http://www.adobe.com/communities/experts/


          • 2. Re: calling array in function
            Damon Edwards Level 3
            Make sure those are the instance names of the MovieClips on stage. Also, make sure each MovieClip has a frame labeled, hidden, inside it.


            • 3. Re: calling array in function
              Damon Edwards Level 3
              Sorry Dave, usual newsgroup delay in post. At least the OP will feel confident now!
              • 4. Re: calling array in function
                4pixels Level 1
                thank you both... that all makes perfect sense and saves me hours trawling through Google and books.

                Can you explain how _root can change though? How would I target something above _parent as well? In websites I always link from / (root) so I can move pages around easily. Is it different in Flash?
                • 5. Re: calling array in function
                  Damon Edwards Level 3
                  If you happen to load your SWF into another SWF, the root timeline can change, unless you use _lockroot = true;. It is much safer to use _parent as many times as you need to keep going up a level. ex:

                  _parent._parent._parent.DoSomething