3 Replies Latest reply on Feb 10, 2010 4:17 AM by Harry Kunz

    Referring to dynamically created movieclips

    bwanakahawa

      Hi,

         I'm having real trouble doing something probably very simple.

       

      I need to dynamically create a group of movieclips which will eventually be options on a menu. The number of projects is a variable, so I tried to create them through a FOR loop.

       

      Code:

       

      numProjects:Number = 3;

       

      for (var j:Number = 1; j <= numProjects; j++) {

           var button:MovieClip = createEmptyMovieClip("button"+j, j);

       

           button.beginFill(0xFF0000, 100);

           button.moveTo(5, 115+(j*20));

           button.lineTo(15, 115+(j*20));

           button.lineTo(15, 125+(j*20));

           button.lineTo(5, 125+(j*20));

           button.endFill();

       

           trace("button "+j+" created");

       

           button.onRelease = function() {

                trace("j = "+j);

           };

      }

       

      The problem is, I get 'j = 4' as the trace for all buttons, so something's obviously wrong...

       

      And how would I later refer to the buttons from another movieclip?

      Could I use this tov remove them all?

       

           for (var j:Number = 1; j <= numProjects; j++) {

                _root.button["button"+j].removeClip();

           }

       

      Sorry for the noob questions but I'm stumped here!

        • 1. Re: Referring to dynamically created movieclips
          Harry Kunz Level 4

          That is because "j" is "global" variable in reference to the function. And at the time you have pressed the button, the loop has already finished long time ago with a final "j" value of 4. Why are you tracing j anyway, as an id value? You may want to get the name of the movieclip or its depth by using:

           

          button.onRelease = function() {

                    trace("mc = " + this._name + " -> has depth of: " + this.getDepth());

               };

          1 person found this helpful
          • 2. Re: Referring to dynamically created movieclips
            bwanakahawa Level 1

            Thanks for that.

             

            Firstly, how come j reaches 4? The for loop tells it to stop when it equals 3.

             

            Secondly, how would I later refer to the buttons using another loop (if I wanted to, say, move them or something)?

             

            Using:

            for (var j:Number = 1; j <= numProjects; j++) {

                 _root."button"+j._x += 200;

            }

             

            throws up the error "Left side of assignment operator must be variable or property"

             

            Cheers,

             

            Derek

            • 3. Re: Referring to dynamically created movieclips
              Harry Kunz Level 4

              Try this simple test

               

               

              for(var n = 1; n <= 3; n++)

              {

                 trace(n);

              }

              trace(n);

               

               

              The loop starts with 1. Each loop cycle ends with incrementing the n value then starts again by checking if it is less than or equal to 3. Then repeats the process until it reaches a value that will not satisfy the condition n <= 3 which is n = 4 and thus signals to exit the for loop. That is why the trace statement after the for loop is 4 which caused the loop to stop.

               

              You can use eval like this:

               

               

              for (var j:Number = 1; j <= numProjects; j++) {

                 var mcButton:MovieClip = eval("_root.button" + j);

                 mcButton._x += 200;

              }

              1 person found this helpful