5 Replies Latest reply on Sep 11, 2014 8:30 AM by Amy Blankenship

    actionscript3: how can I lock x-axis when release something draggable?

    Nicola Cornolti

      I'm really sorry for my bad english.
      I have to do a really strange thing: i have to drag a thing and when I release that, it must lock only on x-axis, not y-axis, and i have to do that on two separate lines. How can I do this?

        • 1. Re: actionscript3: how can I lock x-axis when release something draggable?
          Ned Murphy Adobe Community Professional & MVP

          Can you explain in more detail what you need?  What do you mean that it must lock on the x-axis, not y-axis, when you release it?  Do you drag it only once?  What does it do on the y-axis that you say it is not locked?  When it locks, does it lock to the current x-position, or does it actually move to the x-axis (x = 0) and lock there?

           

          What code do you currently have? What does it do correctly that you want?  What does it not do that you are trying to get it to do?

          • 2. Re: actionscript3: how can I lock x-axis when release something draggable?
            Nicola Cornolti Level 1

            I made a model to make this easier to understand, the ball must be locked on the lines. But i have to drag it on the screen and when I release it, the ball must reposition and lock on one of these lines (the closer)

            lock x-axis.png

            • 3. Re: actionscript3: how can I lock x-axis when release something draggable?
              Ned Murphy Adobe Community Professional & MVP

              Your explanation didn't really clear anything up, and the image gives the impression that nothing is locked on the x-axis but rather the y-axis... it appears the dot can move along the line, so to be able to do that it cannot be locked on the x-axis (horizontal).  If it must be locked to the lines then its y-position is locked to one of two values at any time, but the x-value is not.  If you take the time to address each question I asked and any pothers that might come to mind as you try to then it might be easier to understand how this interface is supposed to be processed.

              1 person found this helpful
              • 4. Re: actionscript3: how can I lock x-axis when release something draggable?
                Nipun Asthana Adobe Employee

                Hi Nicola,

                 

                Based on your sample, you may try this:

                Name your movieclip instance on stage as 'ball' and paste the following code in the actions panel (F9)

                 

                import flash.events.MouseEvent;

                 

                ball.addEventListener(MouseEvent.MOUSE_DOWN, onDown);

                stage.addEventListener(MouseEvent.MOUSE_UP, onUp);

                 

                var y1 = 100, y2 = 200; //Assume y1 and y2 are the y positions of the lines.

                 

                function onDown(e) {

                  stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);

                }

                function onMove(e) {

                  ball.x = stage.mouseX; //For keeping it fixed along the y axis.

                  ball.y = Math.abs(y1-stage.mouseY) < Math.abs(y2-stage.mouseY)? y1: y2;

                }

                function onUp(e) {

                  stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);

                }

                • 5. Re: actionscript3: how can I lock x-axis when release something draggable?
                  Amy Blankenship Level 4

                  Wouldn't it be simpler to just say

                   

                  ball.addEventListener(MouseEvent.MOUSE_DOWN, onDown);

                  stage.addEventListener(MouseEvent.MOUSE_UP, onUp);

                   

                  function onDown(e:Event):void {

                       ball.startDrag(true, new Rectangle(lineMC.x, lineMC.y - (ball.height-lineMC.height)/2, line.width, ball.height));

                  }

                  function onUp(e:Event):void {

                       ball.stopDrag();

                  }

                  This would have the added benefit of keeping the cursor on top of the ball.