home |  
Sell Downloads - Ejunkie
open db network by 19.5 degrees
LYRICS | FREE E-BOOKS | SELL DOWNLOADS WITH PAYPAL
 in   
contribute for fun & profit
brink
-Array functions..
-Flash Remoting ..
-str_pad functio..
-Detecting mouse..
-Ltrim, Rtrim an..
-Opening externa..
-Modifying Flash..
-Smoothing effec..
-Array functions..
-Drawing / Vecto..
 
See all Flash Tutorials
 
-Extensions/Plug..
-FLA..
-Flash Software..
-Flash Tutorials..
 
See all Flash
 
All Resources > Flash > Flash Tutorials > ACTIONSCRIPT TUTORIALS
spread the word around  send this page to a friend   read/write comments/corrections/additions comments  rate this 

Smoothing effect in flash (smooth transition)

by 19.5 Degrees
 
 
views: 11355 | rating: 4/10 | downloads: 2970 download
 




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
}

Similarly you can change other properties like

onClipEvent (enterFrame) {
smooth("_y", 200, 10);
smooth("_xscale", 200, 10);
smooth("_yscale", 200, 10);
smooth("_rotation", 50, 10);

}

Now, to the function "smooth"

movieClip.prototype.smooth = function(prop, final, brake) {

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


download


« PREVIOUS
  INDEX
NEXT »

spread the word around
read comments

untitled
posted by: anonymous
on: Mar 19, 06 10:24 pm

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?

post reply | read replies (0)



untitled
posted by: Ivan
on: Dec 22, 04 2:12 am

GREAT!!!

post reply | read replies (0)



read more commentspost comment 



home | contact | contribute | terms of use | privacy policy |