12 Replies Latest reply on May 6, 2011 4:39 PM by kglad

    For Loop

    juresti Level 1

      I need this code to apply to holder_mc1-5. Does that make sense?

       

      Do I need to make an array?

       

      var j:Number;
      for (j = 0; j < 6; j++) {

       

      trace(j);
                 
      _root["holder_mc"+j].onPress = function() {
          _root["holder_mc"+j].startDrag(false);
              _root["holder_mc"+j].lineStyle(3,0x000000,100);
              _root["holder_mc"+j].lineTo(_root.myWidth,0);
              _root["holder_mc"+j].lineTo(_root.myWidth,_root.myHeight);
              _root["holder_mc"+j].lineTo(0,_root.myHeight);
              _root["holder_mc"+j].lineTo(0,0);
         
          }}

        • 1. Re: For Loop
          kglad Adobe Community Professional & MVP

          change j=0 to j=1:

           


           

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

           

          trace(j);
                     
          _root["holder_mc"+j].onPress = function() {
              _root["holder_mc"+j].startDrag(false);
                  _root["holder_mc"+j].lineStyle(3,0x000000,100);
                  _root["holder_mc"+j].lineTo(_root.myWidth,0);
                  _root["holder_mc"+j].lineTo(_root.myWidth,_root.myHeight);
                  _root["holder_mc"+j].lineTo(0,_root.myHeight);
                  _root["holder_mc"+j].lineTo(0,0);
             
              }}

          • 2. Re: For Loop
            aneelbakshi

            I would suggest.

             

            var j:Number;
            for (j = 0; j < 6; j++) {

             

            trace(j);
            _root["holder_mc"+j].id=j           
            _root["holder_mc"+j].onPress = function() {
                _root["holder_mc"+this.id].startDrag(false);
                    _root["holder_mc"+this.id].lineStyle(3,0x000000,100);
                    _root["holder_mc"+this.id].lineTo(_root.myWidth,0);
                    _root["holder_mc"+this.id].lineTo(_root.myWidth,_root.myHeight);
                    _root["holder_mc"+this.id].lineTo(0,_root.myHeight);
                    _root["holder_mc"+this.id].lineTo(0,0);
               
                }}

            1 person found this helpful
            • 3. Re: For Loop
              kglad Adobe Community Professional & MVP

              that should be:

               


               


               

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

               

              trace(j);
                         
              _root["holder_mc"+j].onPress = function() {

              with(this){
                  startDrag(false);
                     lineStyle(3,0x000000,100);
                    lineTo(_root.myWidth,0);
                     lineTo(_root.myWidth,_root.myHeight);
                     lineTo(0,_root.myHeight);
                     lineTo(0,0);

              }
                 
                  }}

              • 4. Re: For Loop
                juresti Level 1

                They are both correct. So the way I wrote it the loop just went through and did not populate clips 1-5, only 5 worked. Is that right? The way you guys wrote it, the function worked for all clips.

                • 5. Re: For Loop
                  kglad Adobe Community Professional & MVP

                  i wrote it so it works for 1-5.


                  • 6. Re: For Loop
                    aneelbakshi Level 1

                    yes in your case only 5 will work because when u click on i will have the value of  5. Coz when u click loop will be finished his task and i will have the maximum value of loop

                    • 7. Re: For Loop
                      juresti Level 1

                      So when you make the lop var equal to "id" or use the with statement it populates all 1-5?? If you could please give a small explanation I would appreciate it!

                       

                       

                      • 8. Re: For Loop
                        kglad Adobe Community Professional & MVP

                        no, this line determines what objects are affected by the code:

                         

                        for (j = 1; j < 6; j++) {

                         

                        ////////////////////////////////

                         

                        my for-loop runs through j=1,2,3,4,5, which what you requested.  your for-loop was running through j=0,1,2,3,4,5 (and that other poster is making the same error that you were).

                        • 9. Re: For Loop
                          juresti Level 1

                          I'll have to look more at this.

                           

                          This code is working:

                           


                          var j:Number;

                           

                          for (j = 0; j < 7; j++) {
                                
                          eval("holder_mc"+j).onPress = function() {

                           


                          with(this){
                              startDrag(false);
                                 lineStyle(3,0xcccccc,100);
                                lineTo(myWidth,0);
                                 lineTo(myWidth,myHeight);
                                 lineTo(0,myHeight);
                                 lineTo(0,0);
                          }
                          }}

                           


                          I need through 0-5, the code I had before would only draw the line around the last clip which was 5 but yea I ahd to increase the number to 7 but what I'm saying is it only drew a line around the last clip:

                           

                          var j:Number;
                          for (j = 0; j < 6; j++) {

                           

                          trace(j);
                                     
                          _root["holder_mc"+j].onPress = function() {
                              _root["holder_mc"+j].startDrag(false);
                                  _root["holder_mc"+j].lineStyle(3,0x000000,100);
                                  _root["holder_mc"+j].lineTo(_root.myWidth,0);
                                  _root["holder_mc"+j].lineTo(_root.myWidth,_root.myHeight);
                                  _root["holder_mc"+j].lineTo(0,_root.myHeight);
                                  _root["holder_mc"+j].lineTo(0,0);
                             
                              }}

                          • 10. Re: For Loop
                            kglad Adobe Community Professional & MVP

                            use:

                             


                             


                            var j:Number;

                             

                            for (j = 0; j < 6; j++) {
                                  
                            this["holder_mc"+j].onPress = function() {

                             


                            with(this){
                                startDrag(false);
                                   lineStyle(3,0xcccccc,100);
                                  lineTo(myWidth,0);
                                   lineTo(myWidth,myHeight);
                                   lineTo(0,myHeight);
                                   lineTo(0,0);
                            }
                            }}

                             

                             

                            • 11. Re: For Loop
                              juresti Level 1

                              Yes I understand but I don't know exactly why my code does not work I wish someone could explain it ina way a

                              non programmer could get it. I'll look it over Thank you!!

                              • 12. Re: For Loop
                                kglad Adobe Community Professional & MVP

                                use the trace() function to debug your code and decipher what flash is doing:

                                 

                                var j:Number;
                                for (j = 0; j < 6; j++) {

                                 

                                trace(j);  // will trace 0,1,2,3,4,5
                                           
                                _root["holder_mc"+j].onPress = function() {

                                // when this object is pressed j is 5.  ie, the for-loop has already completed and j is at its last value (5).

                                trace(j); // will trace 5 no matter which object is pressed

                                trace(this); // will trace the pressed object

                                trace(this._name.substring(-1));  // will trace the value of j when the pressed object was created.
                                    _root["holder_mc"+j].startDrag(false);
                                        _root["holder_mc"+j].lineStyle(3,0x000000,100);
                                        _root["holder_mc"+j].lineTo(_root.myWidth,0);
                                        _root["holder_mc"+j].lineTo(_root.myWidth,_root.myHeight);
                                        _root["holder_mc"+j].lineTo(0,_root.myHeight);
                                        _root["holder_mc"+j].lineTo(0,0);
                                   
                                    }}