3 Replies Latest reply on Mar 9, 2011 1:06 PM by prodigalmaster

    for loop not working

    prodigalmaster Level 2
      player_mc.onEnterFrame = function(){
      for (i=0; i<4; i++) {
                if (_root["icetw"+i].hitTest(this)) {
                     friction = 0.98;
                } else {
                     friction = 0.92;
                }
           }
      };
      

       

       

      There are 4 movieclips with instance names icetw0, icetw1, icetw2, icetw3.

       

      The code above is correct? but it only works for icetw3. EDIT: Oh wait now it seems to work on none of the movieclips now I add a trace.

       

      Help??what could possibly be wrong with that code? I have other working for loops but this one just doesnt work properly it is so frustrating im thinking its a bug!

        • 1. Re: for loop not working
          kglad Adobe Community Professional & MVP

          1.  are trying to test for a hitTest between player_mc and _root.icetw0, _root.icetw1 etc?

           

          2.  would you notice if and when friction changes by .06?

          1 person found this helpful
          • 2. Re: for loop not working
            Ned Murphy Adobe Community Professional & MVP

            I decided I'd wait to see what you had to say for this one after seeing I got the same results and couldn't find a way around it... the increment (i) gets stuck at 3

            1 person found this helpful
            • 3. Re: for loop not working
              prodigalmaster Level 2

              1. yes

              2. yes but it only for the final movieclip.

              hmm I add a trace if hit and it works for all 4 movieclips but changing a variable (friction) only works for the last movieclip (icetw3).



              if (onIce == true) {
                        friction = 0.98;
                   } else {
                        friction = 0.92;
                   }
                   for (i=0; i<4; i++) {
                        if (_root["ice2_"+i].hitTest(this)) {
                             trace("hit");
                             onIce = true;
                        } else {
                             onIce = false;
                        }
                   }
                   trace(onIce);
              


              I have restarted the code so icetw = ice2_

              This code outputs false until player_mc and ice2_3 are touching. Yet it traces 'hit' if player_mc is touching any of the 4 mc's.

              I got it to work I realised it was the else in the hitTest not working so here is the new code:

               

              if (onIce == true) {
                        friction = 0.98;
                   } else {
                        friction = 0.92;
                   }
                   onIce = false;
                   for (i=0; i<4; i++) {
                        if (_root["ice2_"+i].hitTest(this)) {
                             trace("hit");
                             onIce = true;
                        }
                   }
              

               

              Thanks anyway