# Programming pixels/second deceleration of tween

**URL:** <https://forum.kirupa.com/t/programming-pixels-second-deceleration-of-tween/330397>\
**Category:** flash\
**Created:** [January 14, 2013, 11:02pm UTC](https://forum.kirupa.com/t/programming-pixels-second-deceleration-of-tween/330397 "2013-01-14T23:02:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrewlong](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@andrewlong](https://forum.kirupa.com/u/andrewlong)\
**Post date:** [January 14, 2013, 11:02pm UTC](https://forum.kirupa.com/t/programming-pixels-second-deceleration-of-tween/330397/1 "2013-01-14T23:02:03Z")

</div>

I have a simple project where squares move from one side of the stage to the other. I have to be able to slow them down at a constant rate, specifically in pixels/second (cm/s really, but I’m using a generic conversion rate for it). I figure the easiest way to do this is to have the tweens be the target of a MOTION\_CHANGE event, with a listener that changes the duration. Here is the code:

```auto

//yada yada yada
if(decel>0) tweens[0].addEventListener(TweenEvent.MOTION_CHANGE,decelerate);
//yada yada yada
private function decelerate(e:TweenEvent):void
        {
            var distanceRemaining = e.target.finish - e.target.obj.x;
            var timeRemaining = e.target.duration - e.target.time;
            var decelAmount = decelArray[decel-1]/pixelToCm;//e.g. 0.5 cm/s^2 which equals ~19 px/s^2
            var newTweenLength = distanceRemaining / (distanceRemaining / timeRemaining - decelAmount * e.target.time);//time=distance/speed; speed=previous speed - decelleration * elapsed time
            e.target.duration=newTweenLength;
        }

```

The newTweenLength equation seems to spit out the correct value, which would slow down the tween, but the tween visually speeds up. Why?
