Many times we need to end an action smoothly, like fading out an object or moving
it to a particular position, scaling it etc. I've made a generic function which
give your movie clips this capability without having to deal with any kind of
complex deceleration equations.
Using this you can change any property of your movie clip (_x,_y,_xscale,_yscale,_rotation,_alpha
etc.) using a simple code like
onClipEvent (enterFrame) {
smooth("_x", 100, 10);
// smooth is the function
// "_x" is the property you need to change
// 100 is the final value that you want your object's property (in this case
_x) to have
// 10 is the brake; increase the break to slow down the transition
}
delta = final - this[prop];
delta is the difference between the current value for the specified property
of the object and the final value we want it to have.
if (this[prop]!=final) {
this if condition makes sure that function does not keep on going on once the
object's property has attained the final value.
this[prop] += (Math.round((delta/brake)*10))/10;
this is the main thing. we have to add a "bit" to the object's
property's current value. to calculate that "bit" we have to add, we
divide the delta by the break. next time this statement will be executed, delta
will be smaller than it is this time (as we have added a bit to the property
value), thus the bit calculated next time will be smaller too. this is what causes
the smoothing out effect, as that bit gets smaller with every loop iteration.
now, the reason this simple thing like (delta/break) is enclosed by all that Math.round
and *10 and /10 gibberish is just to round the value to a single decimal place.
for example,
if delta/brake comes out to be 78.7607,
after multiplying it by 10, it becomes 787.607
rounding it makes 788
and dividing it by 10 finally we get 78.8 (see it is rounded to one decimal place)
final note, the more the value of break, the lesser our "bit" will be,
thus transition time will be more.
if (Math.round(delta)==0)
this[prop]=final;
since the "bit" we add is not an integer but has some decimal part
too so we can not be sure that delta will always be 0 in end or some "near
zero" value, so we just round it and check if its near zero, we can assign
the final value to the object's specified property.
} // closing brace for the if
} // closing brace for the function
I'm trying to do a site like http://www.getcosi.com but cannot figure out how to do a smooth transition on a rollover using your code. i'm a newbie, sorry. help?