A Flash Developer Resource Site

# Thread: Change Opacity over Time

1. ## Change Opacity over Time

Hi guys, i'm looking for AS code which will change the opacity of a movie clip by +X over Y time and not go beyone a certain opacity level. I know that that the getTimer() function will need to be used but i'm at a loss as to how to devlop it. I couldn't find the answer on Google.

Any help is appriciated.
Thanks

2. By this I assume you mean alpha. You can do that with the Tween class.

3. suppose you have:

instance ball1_mc which u want to go to opacity 30 in 5 secs
instance ball2_mc which u want to go to opacity 100 in 15 secs

then use

Code:
```
changeOpacity(ball1_mc, 30, 5);
changeOpacity(ball2_mc, 100, 15);

function changeOpacity(movie:MovieClip, opacityFinal:Number, secs:Number):Void {
if(movie._alpha < opacityFinal){movie.increase = true;}else{movie.increase = false;}

movie.opacityChange = Math.abs(movie._alpha - opacityFinal);
movie.opacityRate = 0.4 * movie.opacityChange/secs; //opacity rate change per 0.4 sec
movie.nChange = setInterval(changer, 0.4 * 1000, movie);

function changer(movie:MovieClip):Void {
if(movie.increase){
movie._alpha += movie.opacityRate;
if (movie._alpha >= opacityFinal) {
clearInterval(movie.nChange);
}
}else{
movie._alpha -= movie.opacityRate;
if (movie._alpha <= opacityFinal) {
clearInterval(movie.nChange);
}
}
}
}```
P.S this is not 100% accurate e.g 15 secs may take 15.5 seconds and 30 opacity may end up being 29.4 opacity

eidt: Just saw kotex's reply (ive never used the tween class) damn u can do that with it? lol

#### 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