A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Exponentially Increasing/Decreasing Figure

  1. #1
    Freelance or Bust thodya's Avatar
    Join Date
    Aug 2000
    Toronto, ON

    Exponentially Increasing/Decreasing Figure

    I'm trying to come up with a function that will decrease a number exponentially until it reaches a target number.

    Basically, I'm trying to come up with something that I can use to do those nice actionscripted scales, and slides where the movement starts off fast, then slows quickly as the clip reaches it's target. Could be used for scaling, or sliding.

    I'm thinking the structure of the function would go something like:

    While: CurrentValue is less than TargetValue
    1. Increase/Decrease CurrentValue by FactorX.
    2. Set clip property to CurrentValue.
    3. Increase/Decrease FactorX exponentially.

    I think I have the right approach, but I can't seem to script it properly. Any help would be great. Thanks

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Los Angeles
    This is a very common kind of tween, posted on this board.

    Let's say you are tweening from _x (the current position) to targetX.

    Do something like this every frame, until you get reasonably close to the target:

    speedFactor = .1;
    distance = targetX - _x;
    _x += distance*speedFactor;

    This works if you are decreasing _x to the target OR increasing _x to the target. When _x is decreasing, than the distance value is a negative number.

    Note that if speedFactor were 1, you would jump the entire distance to the target in one big jump. If speedFactor were .5, then you would jump halfway there each step, like the runner in xeno's paradox. But each step, the distance is smaller, so the amount you jump also gets smaller.

    In the above example, I am jumping 1/10th the distance each step. Note that almost everyone else on this board tends to write this like this, using division instead of multiplication:

    _x += (target - _x)/10;

    or even (horrors)

    _x -= (_x - target)/10;

    The result is identical.

    The complete code would look something like this:


    MovieClip.prototype.startTween = function(targetX, speedFactor )
    this.targetX = targetX;
    this.speedFactor = speedFactor;
    this.onEnterFrame = function()
    var distance = this.targetX - this._x;
    if (Math.abs(distance) < 1) {
    this._x = this.targetX;
    delete this.onEnterFrame;
    else {
    this._x += distance*this.speedFactor;

    // Example usage:
    myTargetX = 10;
    mySpeed = .1;
    myMovie.startTween(myTargetX, mySpeed);

    Also see the following thread for info about the built-in (undocumented) tween functions in Flash MX 204.


    And the following thread for a discussion of the advantages and disadvantages of various methods of movement:

    Last edited by jbum; 11-23-2004 at 03:34 PM.

  3. #3
    Freelance or Bust thodya's Avatar
    Join Date
    Aug 2000
    Toronto, ON
    Simple. Beautiful. Thanks.

    Haha I feel like a newbie - I did assume this was a common issue, but I couldn't find the right search terms to bring anything up.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width

HTML5 Development Center