6 Replies Latest reply on Sep 16, 2007 8:32 AM by Juankpro

    for loop + position mc's

    hcciii
      After attaching movie clips to my stage, I am attempting to reposition each clips _x coordinates using a combination for(loop) and onEnterFrame.

      Because I need to reference each movie clip by name, I use a for(loop) to created generate MC names and then store it in a variable called "cp". I attempt to reference the ._x properties using the variable "cp" but it doesn't seem to work. Any suggestions??

      Here is my code:

      // future arrays
      var nodes:Number = 3

      // position movie clips
      for (i:Number = 0; i < nodes; i++) {
      cp = "box"+i
      this.onEnterFrame = function():Void {
      cp._x += (0 - cp._x)/i+1;
      }; //end onE
      }; //end for loop


      Here is the entire block of code from which this potion was taken:

      // future array
      var nodes:Number = 3

      //attach movie clips to stage
      for (i:Number = 0; i < nodes; i++) {
      var d:Number = this.getNextHighestDepth();
      var mc:MovieClip = attachMovie("box", "box"+d, d, {_x: Stage.width, _y: i*83});
      }; //end for loop

      // position movie clips
      for (i:Number = 0; i < nodes; i++) {
      cp = "box"+i
      trace(cp)
      this.onEnterFrame = function():Void {
      cp._x += (0 - cp._x)/i+1;
      }; //end onE
      }; //end for loop
        • 1. for loop + position mc's
          hcciii Level 1
          • 2. Re: for loop + position mc's
            Juankpro
            first of all, this will not work because a movie clip is not a string.
            To reference a movieclip with a dynamically generated name you need to use eval or the [] operators. So cp="box"+i should be cp=this["box" + i].

            Second, even if that is corrected the animation will not work because a variable can hold only one value at a time. It cannot hols all movieclips only the last one.
            And you are using a single onEnterFrame assignmed meny times to the same clip (the root timeline) which does not have any sense either.

            You need to reference each clip, add an onEnterFrame to each clip and use this to reference each one.

            The right way to doit is:

            // position movie clips
            for (i:Number = 0; i < nodes; i++) {
            cp = this["box"+i];
            cp.onEnterFrame = function():Void {
            this._x += (0 - this._x)/i+1;
            }; //end onE
            }; //end for loop
            • 3. Re: for loop + position mc's
              hcciii Level 1
              Thank you. There is much I need to learn. Little by little I hope to be taller one day! Thanks again.
              • 4. Re: for loop + position mc's
                hcciii Level 1
                I guess I don't really understand how for loops work.

                Juankpro addressed my original question with this snip of code:
                // position movie clips
                for (i:Number = 0; i < nodes; i++) {
                cp = this["box"+i];
                cp.onEnterFrame = function():Void {
                this._x += (0 - this._x)/i+1;
                }; //end onE
                }; //end for loop

                I thought by making some modifications I could:
                1) position each clip to a different spot ( i*83) and 2) have each clip move at a differnt speed ( / i)
                this._x += (i*83 - this._x) / i +1;

                I imagined that the first time the for loop ran throught it the destination _x = 0 ( 0*83) at a rate /1 (0+1) and the second time it ran, the destination _x = 83 at a rate of /2 (1+1), and so on...

                But instead it all of the clips move to the last number of the for loop so the destination _x = 4*83 and the rate /5. How can I target each instance to have a unique destination and speed?

                Can anyone follow my train of thought here??
                • 5. Re: for loop + position mc's
                  Juankpro Level 2
                  // position movie clips
                  for (i:Number = 0; i < nodes; i++) {
                  cp = this["box"+i];
                  cp._x = 0;
                  cp.endX = (i * 83);
                  cp.speed = i + 1;
                  cp.onEnterFrame = function():Void {
                  this._x += (this.endX - this._x)/this.speed;
                  }; //end onE
                  }; //end for loop
                  • 6. Re: for loop + position mc's
                    Juankpro Level 2
                    When creating this kind of loops you cannot use the original loop variables because they will always have the last loop value.

                    So the thecnique implies saving a copy of the loop variables into a clip own variables and then use this new properties on the onEnterFrame event.