# Drag return problem

**URL:** <https://forum.kirupa.com/t/drag-return-problem/320786>\
**Category:** flash\
**Created:** [April 1, 2011, 11:37am UTC](https://forum.kirupa.com/t/drag-return-problem/320786 "2011-04-01T11:37:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ukspursbloke](https://avatars.discourse-cdn.com/v4/letter/u/bc8723/32.png) [@ukspursbloke](https://forum.kirupa.com/u/ukspursbloke)\
**Post date:** [April 1, 2011, 11:37am UTC](https://forum.kirupa.com/t/drag-return-problem/320786/1 "2011-04-01T11:37:40Z")

</div>

Hey guys. I’m creating a slider with throw physics that when you drag it to a certain point in the axis in goes to a certain frame - which is working fine.

[FONT=Courier New]

```auto
import fl.transitions.Tween;
import fl.transitions.easing.*;

var bounds:Object = {left:3.05, right:191.5};
var currentX:Number = sliderBtn_mc.x;
var lastX:Number = sliderBtn_mc.x;
var vx:Number = 0;
var isDragging:Boolean = false;
var offset:Number;
sliderBtn_mc.buttonMode = true;

addEventListener(Event.ENTER_FRAME, loop);
sliderBtn_mc.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
addEventListener(MouseEvent.MOUSE_UP, onUp);

function onDown(e:MouseEvent):void
{
   isDragging = true;
   offset = sliderBtn_mc.mouseX;
   addEventListener(MouseEvent.MOUSE_MOVE, onMove);
   function checkSliderX(event:Event):void{
   }
}

function onUp(e:MouseEvent):void
{
   isDragging = false;
   if(sliderBtn_mc.x <= 191.5){
   var myTween:Tween = new Tween(sliderBtn_mc, "x", Regular.easeIn, currentX, 3.5, 0.5, true); 
   }
   removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
}

function onMove(e:MouseEvent):void
{
   sliderBtn_mc.x = mouseX - offset;
   if(sliderBtn_mc.x <= bounds.left)
      sliderBtn_mc.x = bounds.left;
   else if(sliderBtn_mc.x >= bounds.right)
      sliderBtn_mc.x = bounds.right;
      e.updateAfterEvent();
}

function loop(e:Event):void
{
   if(sliderBtn_mc.x >= 191.5){
   MovieClip(root).gotoAndPlay("unlocked");
   removeEventListener(Event.ENTER_FRAME, loop);
   }
   
   if(isDragging)
   {
      lastX = currentX;
      currentX = mouseX;
      vx = currentX - lastX;
   }   
   else
   {
      sliderBtn_mc.x += vx;
   }
   
   if(sliderBtn_mc.x <= bounds.left)
   {
      sliderBtn_mc.x = bounds.left;
   }
   else if(sliderBtn_mc.x >= bounds.right)
   {
      sliderBtn_mc.x = bounds.right;
   }
   
   vx *= 0.6;
   if(Math.abs(vx) < 0.5) vx = 0;
}

```

[/FONT]

However I want the button to return to it’s original position when onUp is triggered, which is why I’ve inserted a tween here:

```auto
function onUp(e:MouseEvent):void
{
 isDragging = false;
 if(sliderBtn_mc.x <= 191.5){
 var myTween:Tween = new Tween(sliderBtn_mc, "x", Regular.easeIn, currentX, 3.5, 0.5, true); 
 }
 removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
}

```

The problem I have is that when onUp is triggered the button jumps to the right before easing back into it’s position. And the closer to the right edge of the button I click, the more it moves. If I click right on the edge it moves the whole button’s width. So obviously currentX isn’t working correctly, and I’ve tried lastX with the same outcome.  
Any help would be much appreciated. Thanks
