7 Replies Latest reply on Apr 14, 2013 2:02 PM by Ned Murphy

    How to create a button loop until mouse is released?

    Mercinova

      How would I go about creating a button loop that continuously increments while the button is being pressed and stops incrementing on release?

        • 1. Re: How to create a button loop until mouse is released?
          Ned Murphy Adobe Community Professional & MVP

          Here is one way using the setInterval function (allows you to control the speed of the increment):

           

          var interval;
          var count = 0;

           

          btn.onPress = function(){
               interval = setInterval(increment, 500);
          }

           

          function increment(){
               count++;
               trace(count);
          }

           

          btn.onRelease = function(){
               clearInterval(interval);
          }

          • 2. Re: How to create a button loop until mouse is released?
            Mercinova Level 1

            Much Thanks, Ned!

             

            I got it to work using this code:

             

            var interval;

               var count=0;

              

            //PlayerCastBar

              //Bar

               //X Pos

               i_Mercinova.PCBBXPDecrease.onPress = function()

                {

                 interval=setInterval(increment, 1);

                }

                function increment()

                {

                 count++;

                 _root.playercastbar.i_Castbar._x=(_root.playercastbar.i_Castbar._x-1)

                }

               i_Mercinova.PCBBXPDecrease.onRelease = function()

                {   

                 clearInterval(interval);

                }

               i_Mercinova.PCBBXPIncrease.onPress = function()

                {

                 interval=setInterval(increment, 1);

                }

                function increment()

                {

                 count++;

                 _root.playercastbar.i_Castbar._x=(_root.playercastbar.i_Castbar._x+1)

                }

               i_Mercinova.PCBBXPIncrease.onRelease = function()

                {   

                 clearInterval(interval);

                }

             

             

            However,  for some reason...both buttons now do the same thing.  One should subtract from the X Pos, while the other should add to the X Pos.  Not sure what the problem is...the code looks right to me.

            • 3. Re: How to create a button loop until mouse is released?
              Ned Murphy Adobe Community Professional & MVP

              YOur code is behaving the way you tell it to. You have two functions named increment.... you can only have one with that name.  If you think one of them is supposed to be decrementing, then you should rename it to that and change the count++ to count--

              • 4. Re: How to create a button loop until mouse is released?
                Mercinova Level 1

                Ok...that makes sense.  So, naming one "incrementPCBBXPDecrease" and the other "incrementPCBBXPIncrease" should correct this problem...

                 

                I wasn't aware that I could rename "increment"...I thought it was a built-in function/class.

                 

                I've got lots to learn! 

                 

                You've been most helpful...Thank You!! 

                • 5. Re: How to create a button loop until mouse is released?
                  Mercinova Level 1

                  That worked perfectly!!  Just renaming "increment" did the job...I didn't change the count++ to count--,

                   

                  Would it hurt to include a declaration to reset var count to 0 in the onRelease function for each button?

                  • 6. Re: How to create a button loop until mouse is released?
                    Mercinova Level 1

                    Just want to post the finished code for anyone who is interested.

                     

                    var interval;

                       var count=0;

                      

                    //PlayerCastBar

                      //Bar

                       //X Pos

                       i_Mercinova.PCBBXPDecrease.onPress = function()

                        {   

                         interval=setInterval(incrementPCBBXPDecrease, .01);

                        }

                        function incrementPCBBXPDecrease()

                        {

                         count++;

                         _root.playercastbar.i_Castbar._x=(_root.playercastbar.i_Castbar._x-2)

                        }

                       i_Mercinova.PCBBXPDecrease.onRelease = function()

                        {   

                         clearInterval(interval);

                         count==0;

                        }

                       i_Mercinova.PCBBXPIncrease.onPress = function()

                        {

                         interval=setInterval(incrementPCBBXPIncrease, .01);

                        }

                        function incrementPCBBXPIncrease()

                        {

                         count++;

                         _root.playercastbar.i_Castbar._x=(_root.playercastbar.i_Castbar._x+2)

                        }   

                       i_Mercinova.PCBBXPIncrease.onRelease = function()

                        {   

                         clearInterval(interval);

                         count==0;

                        }

                     

                     

                    It does exactly what I wanted it to.  Again, Thank You Very Much, Ned!!