2 Replies Latest reply on Feb 25, 2011 7:47 AM by ArthurTheIgnorant

    Holding mouse down

    ArthurTheIgnorant

      Hi

       

      I currently have an issue where as I'd like to repeat an action when a user holds down their mouse button on one of the buttons within my application. At present, I'm using the following:

       

      button_left.onPress = function() {
           // action here
      }
      

       

      This works but only executes the action once; it does not have the recurring effect I'm looking for. I've tried a few things in an attempt to get this working, one of which is below, but all have failed.

       

      var flag1 = false;
      button_left.onPress = function() {
           flag1 = true;
           while(flag1==true) {
                // action
           }
      }
      
      button_left.onRelease = function() {
           flag1 = false;
      }
      

       

      Thanks in advance for any help

        • 1. Re: Holding mouse down
          dtauer Level 2

          You want to use setInterval to do a repeated step. All you need to do is alter the rate at which it's called (i.e. a smaller number will run faster).  Here's the commented code:

           

          var id:Number;
          
          button_left.onPress = function()
          {
               //Stop any existing intervals
               clearInterval(id);
               //Start a new interval. 
               //The "doAction" function will be called at whatever rate you enter
               //Rate is in milliseconds (i.e. 1000 = 1 second)
               id = setInterval(doAction, 1000);
          }
          
          button_left.onRelease = function()
          {
               //Stop the interval
               clearInterval(id);
          }
          
          //When the interval is started, this function will be called 
          //at whatever rate you've choosen
          function doAction():Void
          {
               trace("action");
          }
          

          • 2. Re: Holding mouse down
            ArthurTheIgnorant Level 1

            Worked perfect and very well explained. Thanks very much!