3 Replies Latest reply on Aug 17, 2009 3:14 PM by k.bitgood

    Array Troubles

    k.bitgood

      I'm trying to create movieclips in an array so that I can access them by movie[x], where x is the number of the specific movie. but I'm having some troubles. Here is my code:

      ActionScript Code:
      var movie:Array = new Array();
      for (k=0; k != 7; k++) {
          movie[k] = _root.createEmptyMovieClip("movie"+k+"_mc", this.getNextHighestDepth());
          //code for filling the movie clips with text and other stuff
          if (k>0) {
              movie[k]._y = movie[k-1]._y+movie[k-1]._height;
          }
      }
      

      This isn't working. It should make a vertical list of the movieclips, but its not.
      I tried the following traces:
      ActionScript Code:
      var movie:Array = new Array();
      for (k=0; k != 7; k++) {
          movie[k] = _root.createEmptyMovieClip("movie"+k+"_mc", this.getNextHighestDepth());
          //code for filling the movie clips with text and other stuff
          if (k>0) {
              movie[k]._y = movie[k-1]._y+movie[k-1]._height;
          }
          trace(k); //returns "0" then "1" then "2" ... etc. (this is good)
          trace(k-1); //returns "-1" then "0" then "1" ... etc. (this is good)
          trace(movie[1]); //returns "_level0.movie1_mc"   (this is good)
          trace(movie[k]); //returns "_level0.movie0_mc" then "_level0.movie1_mc" ... etc. (this is also good)
          trace(movie[k-1]); //returns "" nothing (this is bad)
      }
      

      Does any one have any idea why I can't access movie[k-1] ?

        • 1. Re: Array Troubles
          Ned Murphy Adobe Community Professional & MVP

          k-1 is negative on the first round thru, and there will be no movie[-1]

           

          You probably want to put those traces inside the conditional to avoid that.

           

          Aside from that all, your code works just fine as is, so there may be something you are doing inside that... "//code for filling the movie clips with text and other stuff" bit you show if things are not stacking one atop the other. Are you trying to load things into them dynamically using loadMovie or some other method that takes data from the server?  If so, that loop will probably execute before the content is loaded, so it will all be pretty much at y = 0 (or something less than the true final heights of the mc's..

          1 person found this helpful
          • 2. Re: Array Troubles
            k.bitgood Level 1

            Thank you for replying to me.

             

            I have tried those traces in many different locations in the code and I understand that.

             

            I am not using loadMovie or anything like that all I am doing is creating a text field.

             

            Here is my complete code to make sure the proble isn't somewhere else:

             

            var catagory:Array = new Array();
            var word:Array = new Array();
            var movie:Array = new Array();
            var my_fmt = new TextFormat();
            my_fmt.align = "center";
            my_fmt.font = "Segoe UI";
            my_fmt.size = 20;
            
            list_xml.onLoad = function() {
                 c = list_xml.firstChild;
                 for (i=c; i != null; i=i.nextSibling) {
                      numC++;
                      catagory[numC-1] = i.attributes.catagory;
                      w = i.firstChild;
                      for (j=w; j != null; j=j.nextSibling) {
                           numW++;
                           word[numW-1] = [i.attributes.catagory, j.attributes.word];
                      }
                 }
                 for (k=0; k != word.length; k++) {
                      movie[k] = _root.createEmptyMovieClip("word"+k+"_mc", this.getNextHighestDepth());
                      var size:Object = my_fmt.getTextExtent(word[k][1]);
                      movie[k].createTextField("word_txt",this.getNextHighestDepth(),0,0,size.textFieldWidth,size.textFieldHeight);
                      movie[k].word_txt.text = word[k][1];
                      movie[k].word_txt.selectable = false;
                      movie[k].word_txt.setTextFormat(my_fmt);
                      trace(word[k][1]);
                      if (k>0) {
                           movie[k]._y = movie[k-1]._y+movie[k-1]._height;
                      }
                 }
            };
            

            • 3. Re: Array Troubles
              k.bitgood Level 1

              Oh! I found my problem.

              Line 29 was

                        movie[k] = _root.createEmptyMovieClip("word"+k+"_mc", this.getNextHighestDepth());

              when it should have been

                        movie[k] = _root.createEmptyMovieClip("word"+k+"_mc", _root.getNextHighestDepth());