A Flash Developer Resource Site

1. ## Exponentially Increasing/Decreasing Figure

Hey,
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. 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:

code:
```

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:

3. 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.

Cheers!

#### Posting Permissions

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

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap