3 Replies Latest reply: May 27, 2012 4:14 AM by Ned Murphy RSS

    Press and hold button goto

    Electroclash Community Member

      Hi ! I need help to update a script from AS2 to AS3.

       

      I'm simulating a function often found on a phone where you have a button and when you click on it something happens (in that case a gotoAndStop) but when you click and hold for a second that same button other thing happens (in that case another gotoAndStop).

       

      This is my original script doing exactly what I'm looking for:

       

      stop();

      function startTimer(mc, conversionTime) {
          mc
      .onEnterFrame = function() {
             
      if ((getTimer() / 1000) - conversionTime > 1) {
                 
      delete this.onEnterFrame;
                  gotoAndStop
      (3);
             
      }
         
      };
      }
      button1
      .onPress = function() {
         
      var conversionTime:Number = getTimer() / 1000;
          startTimer
      (this, conversionTime);
         
      this.onRelease = function() {
             
      if (this.onEnterFrame != null) {
                  gotoAndStop
      (2);
             
      }
             
      delete this.onEnterFrame;
         
      };
      };

       

       

      Can someone help me to translate it to AS3 ?

      Thank you !

       

        • 1. Re: Press and hold button goto
          Ned Murphy MVP

          Here's one approach to doing what you describe... I didn't deal with the goto statements, but you should be able to fill them in place of the traces...

           

          var quickClick:Boolean;
          var sto:uint;

           

          function changeClickStatus():void {
               quickClick = false;
          }

           

          button1.addEventListener(MouseEvent.MOUSE_DOWN, btnPressed);

           

          function btnPressed(evt:MouseEvent):void {
               quickClick = true;
               button1.addEventListener(MouseEvent.CLICK, makeChoice);
               sto = setTimeout(changeClickStatus,1000);   // delay 1 second
          }

           

          function makeChoice(evt:MouseEvent):void {
               button1.removeEventListener(MouseEvent.CLICK, makeChoice);
               clearTimeout(sto);
               if(quickClick){
                    trace("clicked quickly");
               } else {
                    trace("delayed click");
               }
          }

          • 2. Re: Press and hold button goto
            Electroclash Community Member

            Hi Ned.

             

            Your code works perfectly. You are a master. Fast and precise answers. This community would not be the same without you.

             

            Many thanks !

            William

            • 3. Re: Press and hold button goto
              Ned Murphy MVP

              You're welcome