3 Replies Latest reply on Apr 7, 2009 5:16 PM by David Stiller

    Why doesn't this cycle work?

    shintashi Level 1

      drain = true;
      onEnterFrame = function(){
         
         
         
         
          if(drain == true){water_mc.waterlv_mc._alpha -= 2;}
         
         
          if(water_mc.waterlv_mc._alpha < 3){water_mc.waterlv_mc._alpha += 2; drain = !true;}   
          if(water_mc.waterlv_mc._alpha > 97){drain = true;}

       


         
      }

       

       

      the basic idea is simple, it empties and fills as the frames progress, but It seems to empty and stop when tested. What's going wrong?

        • 1. Re: Why doesn't this cycle work?
          Ned Murphy Adobe Community Professional & MVP

          When the alpha falls below 3, drain switches to false, and then pumps the alpha back above 3, so at this stage, neither of the two actions to increment or decrement are enabled.... drain is false and alpha is no longer < 3.

           

          Try regrouping things as follows:

           

          var drain = true;


          onEnterFrame = function(){
             
          if(drain == true){
            water_mc.waterlv_mc._alpha -= 2;
          } else {
            water_mc.waterlv_mc._alpha += 2;
          }
             
          if(water_mc.waterlv_mc._alpha < 3){
            drain = false;
          } else if(water_mc.waterlv_mc._alpha > 97){
            drain = true;

          }

           

          }

          • 2. Re: Why doesn't this cycle work?
            David Stiller Level 2

            shintashi,

             

                 When code performs in unexpected ways, your best bet is often to do a bit of troubleshooting.  One of the quickest, easiest ways to troubleshoot your code is to use the trace() function, which lets you "see" what's going on in your code.  For example, try adding the following trace statement to your existing code:

             

            drain = true;
            onEnterFrame = function() {
                trace(water_mc.waterlv_mc._alpha);
                if (drain == true) {
                    water_mc.waterlv_mc._alpha -= 2;
                }
                if (water_mc.waterlv_mc._alpha < 3) {
                    water_mc.waterlv_mc._alpha += 2;
                    drain = !true;
                }
                if (water_mc.waterlv_mc._alpha > 97) {
                    drain = true;
                }
            };

             

                 When you test your movie again, you'll see the value of waterlv_mc's _alpha property in the Output panel, which might be an eye-opener for you.  First, you'll notice that the _alpha property doesn't decrement by 2s, which is one of the quirks of ActionScript 1.0/2.0.  The reason for this -- and this is just "one of those things" it's good to know -- is that the _alpha property is stored internally as a value from 0 to 256.  The _alpha property "translates" those internal values for you to a range of 0 to 100, which is why the values veer from perfect integers.

             

                 This is a bit of a tangent, though, because the real reason waterlv_mc never "refills" is that your if() statements aren't actually set up to accomplish what you want them to.  Compare the original code with the following variation:

             

            var drain = true;
            onEnterFrame = function() {
                trace(water_mc.waterlv_mc._alpha);
                if (drain == true) {
                    water_mc.waterlv_mc._alpha -= 2;
                } else {
                    water_mc.waterlv_mc._alpha += 2;
                }
                if (water_mc.waterlv_mc._alpha >= 100) {
                    drain = true;
                }
                if (water_mc.waterlv_mc._alpha <= 0) {
                    drain = false;
                }
            };

             

                 In this case, the first if() statement either decrements or increments (thanks to an else) all in the same statement, depending on the value of drain.  First and foremost, you're either decreasing or increasing the value of waterlv_mc's _alpha property.  After that, another set of if() statements determines the value of drain.  When _alpha is 100 (or higher!) you definitely want to "drain the water," so the drain variable is set to true.  On the other hand, when _alpha is 0 (or lower), you want to "refill," so drain is set to false.

             

                 Let me know if this makes sense to you! 

             

             

            David Stiller
            Contributor, How to Cheat in Adobe Flash CS4
            http://tinyurl.com/dpsCheatFlashCS4
            "Luck is the residue of good design."

            1 person found this helpful
            • 3. Re: Why doesn't this cycle work?
              David Stiller Level 2

              Ned,

               

                   Argh, you beat me!  lol