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:
//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?