6 Replies Latest reply on Dec 10, 2007 4:33 PM by bartonsmith

    Drag and Drop with inertia.

    bartonsmith
      Preview file

      I grabbed this script from senocular.com and modified it to suit what I am doing, which is creating draggable mc's that slide after the mouse is release.
      The problem I am having, and can't seem to solve, is that when you click on the mc it sets the _ x and _y to _xmouse and _ymouse. It is because of this part of the script:

      _x = _parent._xmouse
      _y = _parent._ymouse

      I really want it to act the same as the startDrag function, where the mc doesn't reset its _x and _y coords to _xmouse _ymouse. If you modify it to be

      _x = _parent._xmouse - _x

      it just goes crazy - let alone the fact it only accounts for clicking right of the centre.

      Please help
      Cheers
      Barton
        • 1. Re: Drag and Drop with inertia.
          Damon Edwards Level 3
          There is a parameter in the startDrag method that sets the the drag area to be locked to the center or not. true will lock it to center, like what you have now, or false will allow it drag wherever the mouse is clicked.

          startDrag(this, false, left, top, right, bottom)
          • 2. Re: Drag and Drop with inertia.
            bartonsmith Level 1
            how can i incorporate that startDrag code into mine though? becuase it isn't using the startDrag method...
            • 3. Re: Drag and Drop with inertia.
              scottgroovez Level 1
              One way of doing it would be to store the MovieClip._xmouse and MovieClip._ymouse properties of the movieclip you're dragging, then set the _x and _y to the parent _xmouse and parent._ymouse minus those variables.

              For example:
              mc.onPress = function() {
              var start_x:Number = mc._xmouse;
              var start_y:Number = mc._ymouse;
              mc.onMouseMove = function() {
              this._x = this._parent._xmouse-start_x;
              this._y = this._parent._ymouse-start_y;
              }
              }

              In your scenario you could set these variables when you set the dragging boolean to true.
              • 4. Drag and Drop with inertia.
                bartonsmith Level 1
                sorry are you saying to add that code to the code I attached above? Tried playing with it in a few spots but no dice.
                Is there a way to change x and y coords inside the mc to the _mousex, _mousey coords onPress?
                Ahh I thought this would be easy :\
                Cheers
                • 5. Re: Drag and Drop with inertia.
                  Level 7
                  Wherever you set your dragging variable to true, onPress I assume, you want
                  to record the offset of the mouse click from the left, top edge of the clip.
                  Your draggable clips should have top,left reg points too... Anyway - when
                  you click you want to do something like so:

                  deltaX = _xmouse - dragClip._x;
                  deltaY = _ymouse - dragClip._y;

                  Then when you move you want:

                  _x = _parent._xmouse - deltaX;
                  _y = _parent._ymouse - deltaY;


                  HTH

                  --
                  Dave -
                  Head Developer
                  http://www.blurredistinction.com
                  Adobe Community Expert
                  http://www.adobe.com/communities/experts/


                  • 6. Drag and Drop with inertia.
                    bartonsmith Level 1
                    Hey DMennenoh, I tried apply that code as follows:

                    on(press){
                    dragging = true
                    deltaX = _xmouse - imagemc._x;
                    deltaY = _ymouse - imagemc._y;
                    }

                    onClipEvent (enterFrame) {
                    if (dragging) {
                    _x = _parent._xmouse - deltaX;
                    _y = _parent._ymouse - deltaY;
                    ...

                    but it doens't move. What do you think is going wrong? Cheers


                    Yeh if you put real values into those equations its not gonna come out right? What were you suggesting dragClip was?