4 Replies Latest reply on Sep 3, 2010 5:08 AM by Graham vdR

    How to create unique variables in for … in loops? (AS2)

    Graham vdR Level 1

      Hi there

       

      I have an onEnterFrame function controlling all the movie clips in an array. The motion of each of these movie clips is controlled by a few variables – speed, acceleration, etc – that are modified on each enterFrame. I can't figure out how to create unique variables for each element in the array. At the moment my variables are the same for all the elements, and so the motions for each movie clip are the same.

       

      I have used this code to add my movie clips into the array:

       

      for(i = 0; i < starNumber; i++){
          duplicateMovieClip(star, "star" + i, i);
          starArray.push(this["star" + String(i)]);   
      }

       

      Do I need to do a similar thing to create unique variables for each? Or is there something I need to do in my for(myClip in myArray) loop that is contained in my onEnterFrame function?

       

      I can post my code if that would help (65 lines).

        • 1. Re: How to create unique variables in for … in loops? (AS2)
          Ganesh_Palwe Level 3

          Posting your code can help you get correct answer more quickly.

          • 2. Re: How to create unique variables in for … in loops? (AS2)
            Graham vdR Level 1

            Cool, here it all is. I wasn't sure if there was anything I could leave out for brevity, so I hope this is all clear enough.

             

            The speedX and speedY variables are the same for all the elements in the starArray. I'd like to make them independent, so that each movie clip has its own speed.

             

             

            var i:Number;
            var starArray:Array = [];
            var mc:String;
            var scale:Number;
            var speedXMod:Number;
            var speedYMod:Number;

             

            var starNumber:Number = 1;
            var minSize:Number = 15;
            var maxSize:Number = 80;
            var acceleration:Number = 0.2;
            var minSpeed:Number = 0;
            var maxSpeed:Number = 1;


            for(i = 0; i < starNumber; i++){
                duplicateMovieClip(star, "star" + i, i);
                starArray.push(this["star" + String(i)]);   
            }

             

            for(mc in starArray){
               
                starArray[mc]._x = (Math.random() * Stage.width);
                starArray[mc]._y = (Math.random() * Stage.height);
                scale = (minSize + (Math.random() * (maxSize - minSize)));
                starArray[mc]._xscale = scale;
                starArray[mc]._yscale = scale;
               
                var startSpeedX:Number = ((Math.pow(-1, (Math.round(Math.random())))) * (minSpeed + (Math.random() * (maxSpeed - minSpeed))));
                var startSpeedY:Number = ((Math.pow(-1, (Math.round(Math.random())))) * (minSpeed + (Math.random() * (maxSpeed - minSpeed))));
               
                var speedX:Number = startSpeedX;
                var speedY:Number = startSpeedY;
               
                onEnterFrame = function(){
                    for(mc in starArray){
                           
                        speedXMod = ((Math.random() * acceleration) - (0.5 * acceleration));
                        speedYMod = ((Math.random() * acceleration) - (0.5 * acceleration));
                        if(((Math.abs(speedX + speedXMod)) < maxSpeed) && ((Math.abs(speedX + speedXMod)) > minSpeed)){
                            speedX += speedXMod;
                        }else{
                            speedX -= speedXMod;
                        }
                        if(((Math.abs(speedY + speedYMod)) < maxSpeed) && ((Math.abs(speedY + speedYMod)) > minSpeed)){
                            speedY += speedYMod;
                        }else{
                            speedY -= speedYMod;
                        }
                       
                        if(((starArray[mc]._x + speedX) > 0) && ((starArray[mc]._x + speedX) < Stage.width)){
                            starArray[mc]._x += speedX;
                        }else{
                            speedX *= -1;
                            starArray[mc]._x += speedX;
                        }
                        if(((starArray[mc]._y + speedY) > 0) && ((starArray[mc]._y + speedY) < Stage.height)){
                            starArray[mc]._y += speedY;
                        }else{
                            speedY *= -1;
                            starArray[mc]._y += speedY;
                        }
                    }
                }
            }

            • 3. Re: How to create unique variables in for … in loops? (AS2)
              Ganesh_Palwe Level 3

              Creating unique variables is easy way out. Following is the modified code using unique variables that should solve your problem:

               

              var i:Number;

              var starArray:Array = [];

              var starArray_X:Array = [];

              var starArray_Y:Array = [];

               

              var mc:String;

              var scale:Number;

              var speedXMod:Number;

              var speedYMod:Number;

               

              var starNumber:Number = 10;

              var minSize:Number = 15;

              var maxSize:Number = 80;

              var acceleration:Number = 0.2;

              var minSpeed:Number = 0;

              var maxSpeed:Number = 1;

               

              for(i = 0; i < starNumber; i++){

                  duplicateMovieClip(star, "star" + i, i);

                  starArray.push(this["star" + String(i)]);   

              }

               

              for(mc in starArray){

               

                  starArray[mc]._x = (Math.random() * Stage.width);

                  starArray[mc]._y = (Math.random() * Stage.height);

                  scale = (minSize + (Math.random() * (maxSize - minSize)));

                  starArray[mc]._xscale = scale;

                  starArray[mc]._yscale = scale;

               

                  var startSpeedX:Number = ((Math.pow(-1, (Math.round(Math.random())))) * (minSpeed + (Math.random() * (maxSpeed - minSpeed))));

                  var startSpeedY:Number = ((Math.pow(-1, (Math.round(Math.random())))) * (minSpeed + (Math.random() * (maxSpeed - minSpeed))));

               

                  starArray_X[mc] = startSpeedX;

                  starArray_Y[mc] = startSpeedY;

               

               

                   onEnterFrame = function(){

                      for(mc in starArray){

               

                          speedXMod = ((Math.random() * acceleration) - (0.5 * acceleration));

                          speedYMod = ((Math.random() * acceleration) - (0.5 * acceleration));

                          if(((Math.abs(starArray_X[mc] + speedXMod)) < maxSpeed) && ((Math.abs(starArray_X[mc] + speedXMod)) > minSpeed)){

                              starArray_X[mc] += speedXMod;

                          }else{

                              starArray_X[mc] -= speedXMod;

                          }

                          if(((Math.abs(starArray_Y[mc] + speedYMod)) < maxSpeed) && ((Math.abs(starArray_Y[mc]+ speedYMod)) > minSpeed)){

                              starArray_Y[mc] += speedYMod;

                          }else{

                              starArray_Y[mc] -= speedYMod;

                          }

               

                          if(((starArray[mc]._x + starArray_X[mc]) > 0) && ((starArray[mc]._x + starArray_X[mc]) < Stage.width)){

                              starArray[mc]._x += starArray_X[mc];

                          }else{

                              starArray_X[mc] *= -1;

                              starArray[mc]._x += starArray_X[mc];

                          }

                          if(((starArray[mc]._y + starArray_Y[mc]) > 0) && ((starArray[mc]._y + starArray_Y[mc]) < Stage.height)){

                              starArray[mc]._y += starArray_Y[mc];

                          }else{

                              starArray_Y[mc] *= -1;

                              starArray[mc]._y += starArray_Y[mc];

                          }

                      }

                  }

              }

              • 4. Re: How to create unique variables in for … in loops? (AS2)
                Graham vdR Level 1

                Beautiful! Thank you very much.