How to fade a button with AS?

I’ve put this code onto a button

on (rollOver) {
testbutton._alpha = 20
}
on (rollOut) {
testbutton._alpha = 100
}

How can I make the fade smooth from 100% to 20% on the rollover and then smooth back out on the rollout?

Any help is appreciated

Well you probably want to make a movieclip act as a button.
Just make a movieclip consisting of three frames. The first frame just have the button without any rollover or rollout effects. The second frame put a movieclip of the same buttonbut use a motion tween to smoothly change the alpha of the button. Then put the 3d state as a duplicate of the button again but make it start at the lower alpha and make a motion tween change the alpha to 100 %. Here is the action script to attach to the main button (that contains the other 3 movieclips):

[AS]
manual.onRollOver = manual.onRelease = function(){
this.gotoAndStop(2)
}
manual.onRollOut = manual.onReleaseOutside = function(){
this.gotoAndStop(1)
}
manual.onPress = function(){
this.gotoAndStop(3)
}
[AS]

Just put this in the first frame and replace all the instances ‘manual’ with whatever the main movieclip’s instance name is. Just so you know this is making movieclips act as buttons :slight_smile:

Mike

try something like

Fade Out:

this.onEnterFrame = function() {
if(testbutton._alpha > 20) {
testbutton._alpha -= 5;
} else {
testbutton._alpha = 20; //just to be sure but maybe not necessary
this.onEnterFrame = null;
}
}

Fade In:

this.onEnterFrame = function() {
if(testbutton._alpha < 100) {
testbutton._alpha += 5;
} else {
this.onEnterFrame = null;
}
}

I typed that really fast but I think it’s right. You can adjust the rate of fade by change “5”. Less is slower, greater is faster.

Hope that helps.

(I think that if you put this code into a button instance then “this” will actually point to the button’s parent. SHould still work however) If you are worried about conflicts of onEnterFrame you could use setInterval instead You can also make “this” “_root” or “myMovieClip_mc” instead as well.

thanks for the replies. I’ll try both methods to see which works better for me

ActionScripting it is the best way to do it in my opinion.

Voetsjoeba, do you have a better way to do it via AS? I tried the solution put forth by dlenner2. It works but I must have done something wrong because the fade ends abrubtly on the rollOver. Any suggestions? Also, in your opinion is it better to use a button or a mc acting as a button? Thanks

Hi,

Glad you got thinks working. I did paste the code you posted directly into an fla and it works fine. I don’t experience the cumulative fading that you talked about above. That’s weird!