Skip to content

Speed and Spring

by Colin on June 10th, 2009

Back in the Flash 5 days, before tweening engines and Robert Penner's easing functions, flash developers used a "dirty" way of tweening through code.

The reason why this was dirty was because in all actuality, the object never completely arrives to it's designation, only fractions upon fractions to being so close to it that the user couldn't tell the difference. Here's a quick little way of remembering how to do that. The following code uses speed and spring.

  1.  
  2. var destX:int = 500;
  3. var speed:Number = 0.5;
  4. var spring:Number = 0.5;
  5. var curX:Number = 0;
  6. var velX:Number = 0;
  7.  
  8. stage.addEventListener (Event.ENTER_FRAME, moveBox);
  9. function moveBox (e:Event):void
  10. {
  11. velX = ((destX - curX) * speed) + (velX * spring);
  12. curX += velX;
  13.  
  14. // this kills the enter frame if the object gets close enough
  15. if (Number(Math.abs(velX).toFixed(2))<0.01) {
  16. trace ('done');
  17. velX=0;
  18. curX=destX;
  19. stage.removeEventListener (Event.ENTER_FRAME, moveBox);
  20. }
  21. your_object.x=curX;
  22. }
  23.  

I'll further explain the code (and math) once I get a chance to.

From → Flash

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS